﻿Imports System.Globalization

Partial Public Class Vessel_Log_Sheet_In
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Dim clsFc As New ClassFunction
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (IsPostBack) Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "VESSEL LOG SHEET "

            txtPlanBerthFrom.Text = Date.Now.ToString("dd MMM yyyy", New CultureInfo("en-US"))
            txtPlanBerthTo.Text = Date.Now.ToString("dd MMM yyyy", New CultureInfo("en-US"))

            ViewState("TugEdit") = ""
            LoadBOoking()
            txtBookingVPS.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSearchVPS.ClientID + "')")
            txtShipVPS.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSearchVPS.ClientID + "')")
            txtProductSearch.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSearchVPS.ClientID + "')")
        End If
    End Sub

    Private Sub LoadBOoking()
        PanelBookingItem.Visible = True
        PanelLogSheet.Visible = False
        Dim ds As DataSet
        Dim DateFrom As String = ""
        Dim DateTo As String = ""
        If (txtDateSearch.Text.Trim <> "") And (txtDateToSearch.Text.Trim() <> "") Then
            DateFrom = txtDateSearch.Text.Split(" ")(2) & CDate(txtDateSearch.Text).ToString("MMdd")
            DateTo = txtDateToSearch.Text.Split(" ")(2) & CDate(txtDateToSearch.Text).ToString("MMdd")
        End If
        If (ddlStatusVPS.SelectedValue = "C") Then
            ds = clsGet.Get_Booking("", "", "", "'CV','CC','CM','CR','CA'", True, ddlPORTVPS.SelectedValue.Replace("0", ""), txtShipVPS.Text, "", "", txtBookingVPS.Text, ddlDestinationSearch.SelectedValue, DateFrom, DateTo, txtProductSearch.Text)
        ElseIf (ddlStatusVPS.SelectedValue = "J") Then
            ds = clsGet.Get_Booking("", "", "", "'FN','OA'", True, ddlPORTVPS.SelectedValue.Replace("0", ""), txtShipVPS.Text, "", "", txtBookingVPS.Text, ddlDestinationSearch.SelectedValue, DateFrom, DateTo, txtProductSearch.Text)
        ElseIf (ddlStatusVPS.SelectedValue = "A") Then
            ds = clsGet.Get_Booking("", "", "", "'MO'", True, ddlPORTVPS.SelectedValue.Replace("0", ""), txtShipVPS.Text, "", "", txtBookingVPS.Text, ddlDestinationSearch.SelectedValue, DateFrom, DateTo, txtProductSearch.Text)
        Else
            ds = clsGet.Get_Booking("", "", "", "'MO'", True, ddlPORTVPS.SelectedValue.Replace("0", ""), txtShipVPS.Text, "", "", txtBookingVPS.Text, ddlDestinationSearch.SelectedValue, DateFrom, DateTo, txtProductSearch.Text)

            Dim dsTemp As DataSet = ds.Copy
            Dim count As Integer = 0
            For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                Dim dtStatus As DataTable = clsGet.Get_Booking_LogSheet_Out(ds.Tables(0).Rows(i).Item("Booking_no"))
                If (ddlStatusVPS.SelectedValue = "L") Then
                    If (dtStatus.Rows.Count > 0) Then
                        If ((dtStatus.Rows(0).Item("Status") & "" = "C") Or (dtStatus.Rows(0).Item("Status") & "" = "A") Or (dtStatus.Rows(0).Item("Status") & "" = "S")) Then
                            dsTemp.Tables(0).Rows(i - count).Delete()
                            dsTemp.AcceptChanges()
                            count += 1
                        End If
                    End If
                ElseIf (ddlStatusVPS.SelectedValue = "U") Then
                    If (dtStatus.Rows.Count > 0) Then
                        If (Not (dtStatus.Rows(0).Item("Status") & "" = "S")) Then
                            dsTemp.Tables(0).Rows(i - count).Delete()
                            dsTemp.AcceptChanges()
                            count += 1
                        End If
                    Else
                        dsTemp.Tables(0).Rows(i - count).Delete()
                        dsTemp.AcceptChanges()
                        count += 1
                    End If
                ElseIf (ddlStatusVPS.SelectedValue = "T") Then
                    dtStatus = clsGet.Get_Load_Unload_Vessel(ds.Tables(0).Rows(i).Item("Booking_no"))
                    If (dtStatus.Rows.Count > 0) Then
                        If (dtStatus.Rows(0).Item("Status") & "" = "A") Then
                            dsTemp.Tables(0).Rows(i - count).Delete()
                            dsTemp.AcceptChanges()
                            count += 1
                        End If
                    Else
                        dsTemp.Tables(0).Rows(i - count).Delete()
                        dsTemp.AcceptChanges()
                        count += 1
                    End If
                Else 'Section
                    If (dtStatus.Rows.Count > 0) Then
                        If (Not (dtStatus.Rows(0).Item("Status") & "" = "C")) Then
                            dsTemp.Tables(0).Rows(i - count).Delete()
                            dsTemp.AcceptChanges()
                            count += 1
                        End If
                    Else
                        dsTemp.Tables(0).Rows(i - count).Delete()
                        dsTemp.AcceptChanges()
                        count += 1
                    End If
                End If
            Next
            ds = dsTemp
            ds.AcceptChanges()
        End If

        If (txtPlanBerthFrom.Text <> "") And (txtPlanBerthTo.Text <> "") Then
            Dim c As Integer = 0
            Dim dtTemp As DataTable = ds.Tables(0).Copy
            For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                Dim dtLogin As DataTable = clsGet.Get_BookingWharf(ds.Tables(0).Rows(i).Item("Booking_no"))
                Try
                    If (CDate(dtLogin.Rows(0).Item("Date_From")) > CDate(txtPlanBerthFrom.Text)) Then
                        If (CDate(dtLogin.Rows(0).Item("Date_From")) > CDate(txtPlanBerthTo.Text)) Then
                            dtTemp.Rows(i - c).Delete()
                            dtTemp.AcceptChanges()
                            c += 1
                        End If
                    ElseIf (CDate(dtLogin.Rows(0).Item("Date_To")) < CDate(txtPlanBerthFrom.Text)) Then
                        dtTemp.Rows(i - c).Delete()
                        dtTemp.AcceptChanges()
                        c += 1
                    End If
                Catch ex As Exception
                    dtTemp.Rows(i - c).Delete()
                    dtTemp.AcceptChanges()
                    c += 1
                End Try
            Next
            ds.Tables.Clear()
            ds.Tables.Add(dtTemp.Copy)
            ds.AcceptChanges()
        End If

        If (ddlProductLD.SelectedValue <> "A") Then
            Dim dsTemp As DataSet = ds.Copy
            Dim count As Integer = 0
            For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                Dim dt As DataTable = clsGet.Get_Booking_Product(ds.Tables(0).Rows(i).Item("Booking_no"))
                If (dt.Rows.Count > 0) Then
                    If (dt.Rows(0).Item("Product_Event") & "" <> ddlProductLD.SelectedValue) Then
                        dsTemp.Tables(0).Rows(i - count).Delete()
                        dsTemp.AcceptChanges()
                        count += 1
                    End If
                Else
                    dsTemp.Tables(0).Rows(i - count).Delete()
                    dsTemp.AcceptChanges()
                    count += 1
                End If
            Next
            ds = dsTemp
        End If
        ds.AcceptChanges()
        If (ds.Tables(0).Rows.Count > 0) Then
            ViewState("LogSheet") = ds
            gridItem.DataSource = ViewState("LogSheet")
            gridItem.DataBind()
            lbAlert.Text = ""
        Else
            gridItem.DataSource = Nothing
            gridItem.DataBind()
            lbAlert.Text = "ไม่พบรายการ Booking"
        End If
    End Sub

    Public Sub Paginate(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim CurrentIndex As Integer = gridItem.PageIndex
        Select Case e.CommandArgument.ToString().ToLower()
            Case "first"
                gridItem.PageIndex = 0
            Case "prev"
                If (gridItem.PageIndex <> 0) Then
                    gridItem.PageIndex = CurrentIndex - 1
                End If
            Case "next"
                If (gridItem.PageIndex <> gridItem.PageCount) Then
                    gridItem.PageIndex = CurrentIndex + 1
                End If
            Case "last"
                gridItem.PageIndex = gridItem.PageCount
        End Select
        gridItem.DataSource = ViewState("LogSheet")
        gridItem.DataBind()
        ViewState("Load") = "Not"
        Timer1_Tick(sender, e)
    End Sub

    Protected Sub ddlPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Page As GridViewRow = gridItem.BottomPagerRow
        Dim ddlPage As DropDownList = CType(Page.Cells(0).FindControl("ddlPage"), DropDownList)
        gridItem.PageIndex = ddlPage.SelectedIndex
        gridItem.DataSource = ViewState("LogSheet")
        gridItem.DataBind()
        ViewState("Load") = "Not"
        Timer1_Tick(sender, e)
    End Sub

    Private Sub gridItem_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridItem.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (gridItem.PageIndex <> 0) Then
            Index = Index Mod (gridItem.PageIndex * 10)
        End If
        If e.CommandName = "Edit" Then
            TabLogSheet.ActiveTabIndex = 0
            PanelBookingItem.Visible = False
            PanelLogSheet.Visible = True
            ViewState("Tug") = CreateDataTableTug()
            ViewState("Booking_no") = gridItem.DataKeys(Index).Values("Booking_no")
            lbName.Text = gridItem.DataKeys(Index).Values("Vehicle_Text")
            lbLOA.Text = gridItem.DataKeys(Index).Values("Vehicle_LOA")
            lbFlag.Text = gridItem.DataKeys(Index).Values("Vehicle_CNTRY")
            lbMaxDraft.Text = gridItem.DataKeys(Index).Values("Vehicle_DWT")
            ViewState("Lay_Time") = gridItem.DataKeys(Index).Values("Lay_Time")
            If (gridItem.DataKeys(Index).Values("Status") & "" = "OA") Then
                btnSave.Visible = False
                btnSaveOut.Visible = False
                ' btnComplete.Visible = False
            Else
                btnSave.Visible = True
                btnSaveOut.Visible = True
                'btnComplete.Visible = True
            End If
            If (Session("Role") = "00014") Then
                btnSave.Visible = True
            End If
            gridProduct.DataSource = Nothing
            gridProduct.DataBind()
            Dim dt As DataTable = clsGet.Get_BookingWharf(ViewState("Booking_no"))
            If (dt.Rows.Count > 0) Then
                lbWharf.Text = dt.Rows(0).Item("Wharf") & ""
            End If
            Dim str As String = clsGet.Get_Wharf_Shifting(ViewState("Booking_no"))
            If (str <> "") Then
                Try
                    lbHShifting.Text = str.Split("-")(0) & " :"
                    lbShifting.Text = str.Split("-")(1)
                    rowShifting.Visible = True
                Catch ex As Exception
                    rowShifting.Visible = False
                End Try
            Else
                rowShifting.Visible = False
            End If


            dt = clsGet.Get_Booking_LogSheet(ViewState("Booking_no"))
            If (dt.Rows.Count > 0) Then
                txtCS.Text = dt.Rows(0).Item("C_S") & ""
                txtDraftFor.Text = dt.Rows(0).Item("Draft_FOR") & ""
                txtDraftAFT.Text = dt.Rows(0).Item("Draft_AFT") & ""
                txtIMO.Text = dt.Rows(0).Item("IMO_no") & ""
            Else
                txtCS.Text = String.Empty
                txtDraftFor.Text = String.Empty
                txtDraftAFT.Text = String.Empty
                txtIMO.Text = String.Empty
            End If

            lbAgent.Text = gridItem.DataKeys(Index).Values("MAgent_name")
            LoadDataVesselLogSheetIn()
        ElseIf e.CommandName = "Detail" Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " document.getElementById('ctl00_contentplaceholder1_IframeDetailPopup').src = '../FormPopup/Detail_VesselBerth_Popup.aspx';", True)
            Session("Booking_no_Detail") = gridItem.DataKeys(Index).Values("Booking_no")
            ModalPopupExtender5.Show()
        End If
    End Sub

    Private Sub LoadDataVesselLogSheetIn()
        ViewState("Tug") = clsGet.Get_Booking_LogSheet_Tug(ViewState("Booking_no"), "I", "")
        Dim dt As DataTable = clsGet.Get_Booking_LogSheet_In(ViewState("Booking_no"))
        If (dt.Rows.Count > 0) Then
            txtCommence.Text = dt.Rows(0).Item("Commence_Date")
            txtTimeCommence.Text = dt.Rows(0).Item("Commence_Time")
            txtRemarkCommence.Text = dt.Rows(0).Item("Commence_Remark")
            txtLast.Text = dt.Rows(0).Item("Last_Value")
            txtRemarkLast.Text = dt.Rows(0).Item("Last_Remark")
            txtETA.Text = dt.Rows(0).Item("ETA_Date")
            txtTimeETA.Text = dt.Rows(0).Item("ETA_Time")
            txtRemarkETA.Text = dt.Rows(0).Item("ETA_Remark")
            txtStart.Text = dt.Rows(0).Item("Start_Value")
            txtRemarkStart.Text = dt.Rows(0).Item("Start_Remark")
            txtAnchor.Text = dt.Rows(0).Item("Anchor_Date")
            txtTimeAnchor.Text = dt.Rows(0).Item("Anchor_Time")
            txtRemarkAnchor.Text = dt.Rows(0).Item("Anchor_Remark")
            txtPosition.Text = dt.Rows(0).Item("Position_Value")
            txtRemarkPosition.Text = dt.Rows(0).Item("Position_Remark")
            txtTugRoping.Text = dt.Rows(0).Item("Tug_Value")
            txtTugRopingRemark.Text = dt.Rows(0).Item("Tug_Remark")
            txtPilot.Text = dt.Rows(0).Item("Pilot")
            txtPOB.Text = dt.Rows(0).Item("POB_Date")
            txtTimePOB.Text = dt.Rows(0).Item("POB_Time")
            txtRemarkPOB.Text = dt.Rows(0).Item("POB_Remark")
            txtSchedule.Text = dt.Rows(0).Item("Schedule_Date")
            txtTimeSchedule.Text = dt.Rows(0).Item("Schedule_Time")
            txtRemarkSchedule.Text = dt.Rows(0).Item("Schedule_Remark")
            txtBerthing.Text = dt.Rows(0).Item("Berthing_Date")
            txtTimeFromBerthing.Text = dt.Rows(0).Item("Berthing_TimeFrom")
            txtTimeToBerthing.Text = dt.Rows(0).Item("Berthing_TimeTo")
            txtRemarkBerthing.Text = dt.Rows(0).Item("Berthing_Remark")
            txtAddition.Text = dt.Rows(0).Item("Addition")
            txtNor.Text = dt.Rows(0).Item("NOR_Date") & ""
            txtNOrTime.Text = dt.Rows(0).Item("NOR_Time") & ""
            txtUpdate.Text = dt.Rows(0).Item("Up_Date") & ""
            txtupTime.Text = dt.Rows(0).Item("Up_Time") & ""
            Dim User_Modify As String = dt.Rows(0).Item("User_Modify")
            lbSIGNATURE.Text = clsGet.Get_NameUser(User_Modify.Split("$")(0))
            Try
                lbSIGNATURE.Text &= User_Modify.Split("$")(1)
            Catch ex As Exception

            End Try
        Else
            txtCommence.Text = String.Empty
            txtTimeCommence.Text = String.Empty
            txtRemarkCommence.Text = String.Empty
            txtLast.Text = String.Empty
            txtRemarkLast.Text = String.Empty
            txtETA.Text = String.Empty
            txtTimeETA.Text = String.Empty
            txtRemarkETA.Text = String.Empty
            txtStart.Text = String.Empty
            txtRemarkStart.Text = String.Empty
            txtAnchor.Text = String.Empty
            txtTimeAnchor.Text = String.Empty
            txtRemarkAnchor.Text = String.Empty
            txtPosition.Text = String.Empty
            txtRemarkPosition.Text = String.Empty
            txtTugRoping.Text = String.Empty
            txtTugRopingRemark.Text = String.Empty
            txtPilot.Text = String.Empty
            txtPOB.Text = String.Empty
            txtTimePOB.Text = String.Empty
            txtRemarkPOB.Text = String.Empty
            txtSchedule.Text = String.Empty
            txtTimeSchedule.Text = String.Empty
            txtRemarkSchedule.Text = String.Empty
            txtBerthing.Text = String.Empty
            txtTimeFromBerthing.Text = String.Empty
            txtTimeToBerthing.Text = String.Empty
            txtRemarkBerthing.Text = String.Empty
            txtAddition.Text = String.Empty
            txtUpdate.Text = String.Empty
            txtupTime.Text = String.Empty
            txtNor.Text = String.Empty
            txtNOrTime.Text = String.Empty
            Dim User_Modify As String = String.Empty
            lbSIGNATURE.Text = String.Empty
        End If
        gridTug.DataSource = ViewState("Tug")
        gridTug.DataBind()
    End Sub
    Private Sub LoadDataVesselLogSheetOUt()
        ViewState("Tug") = clsGet.Get_Booking_LogSheet_Tug(ViewState("Booking_no"), "O", "")
        Dim dt As DataTable = clsGet.Get_Load_Unload_Product(ViewState("Booking_no"), "")
        If (dt.Rows.Count > 0) Then
            gridProduct.DataSource = dt
            gridProduct.DataBind()
        End If
        dt = clsGet.Get_Load_Unload_Vessel(ViewState("Booking_no"))
        If (dt.Rows.Count > 0) Then
            txtLastLine.Text = dt.Rows(0).Item("Depart_Actual_Date") & ""
            txtTimeLastLine.Text = dt.Rows(0).Item("Depart_Actual_Time") & ""
        End If
        dt = clsGet.Get_Booking_LogSheet_Out(ViewState("Booking_no"))
        If (dt.Rows.Count > 0) Then
            txtPilotOut.Text = dt.Rows(0).Item("Pilot")
            txtPOBOut.Text = dt.Rows(0).Item("POB_Date")
            txtTimePOBOut.Text = dt.Rows(0).Item("POB_Time")
            txtRemarkPOBOut.Text = dt.Rows(0).Item("POB_Remark")
            txtTugRopingOut.Text = dt.Rows(0).Item("Tug_Value")
            txtRemarkTugRopingOut.Text = dt.Rows(0).Item("Tug_Remark")
            txtLastLine.Text = dt.Rows(0).Item("Last_Date")
            txtTimeLastLine.Text = dt.Rows(0).Item("Last_Time")
            'txtTimeLastLineFrom.Text = dt.Rows(0).Item("Last_TimeFrom") & ""
            txtRemarkLastLine.Text = dt.Rows(0).Item("Last_Remark")
            txtNext.Text = dt.Rows(0).Item("Next_Value")
            txtRemarkNext.Text = dt.Rows(0).Item("Next_Remark")
            txtEnd.Text = dt.Rows(0).Item("End_Date")
            txtTimeEnd.Text = dt.Rows(0).Item("End_Time")
            txtRemarkEnd.Text = dt.Rows(0).Item("End_Remark")
            txtAdditionOut.Text = dt.Rows(0).Item("Addition")

            'Dim User_Modify As String = dt.Rows(0).Item("User_Modify")
            'lbSIGNATUREOut.Text = clsGet.Get_NameUser(User_Modify.Split("$")(0))
            'Try
            '    lbSIGNATUREOut.Text &= User_Modify.Split("$")(1)
            'Catch ex As Exception

            'End Try

            If (dt.Rows(0).Item("Status") & "" = "C") Then
                btnCheck.Visible = False
                btnSend.Visible = False
                If (Session("Role") = "00014") Then
                    btnApprove.Visible = True
                    btnReject.Visible = True
                    btnSaveOut.Visible = True
                Else
                    btnReject.Visible = False
                    btnApprove.Visible = False
                    btnSaveOut.Visible = False
                End If

            ElseIf (dt.Rows(0).Item("Status") & "" = "S") Then
                btnApprove.Visible = False
                btnSend.Visible = False
                If (Session("Role") = "00014") Or (Session("Role") = "00013") Then
                    btnCheck.Visible = True
                    btnReject.Visible = True
                Else
                    btnReject.Visible = False
                    btnSaveOut.Visible = True
                    btnCheck.Visible = False
                End If

            ElseIf (dt.Rows(0).Item("Status") & "" = "A") Then
                btnCheck.Visible = False
                btnApprove.Visible = False
                btnSend.Visible = False
                btnReject.Visible = False
                If (Session("Role") = "00014") Then
                    btnSaveOut.Visible = True
                    btnSave.Visible = True
                Else
                    btnSaveOut.Visible = False
                    btnSave.Visible = False
                End If
            Else
                btnSaveOut.Visible = True
                btnCheck.Visible = False
                btnApprove.Visible = False
                btnReject.Visible = False
                btnSend.Visible = True
            End If

            dt = clsGet.Get_Action_Operation(ViewState("Booking_no"))
            Dim Status As String = ""
            For Each dx As DataRow In dt.Rows
                If (Status = "") Then
                    Status = dx.Item("A_Action") & " : " & dx.Item("A_By") & " " & dx.Item("A_Date") & " " & dx.Item("A_Time") & " " & dx.Item("A_Description")
                Else
                    Status &= "<br />" & dx.Item("A_Action") & " : " & dx.Item("A_By") & " " & dx.Item("A_Date") & " " & dx.Item("A_Time") & " " & dx.Item("A_Description")
                End If
            Next
            LiStatus.Text = Status
        Else
            txtPilotOut.Text = String.Empty
            txtPOBOut.Text = String.Empty
            txtTimePOBOut.Text = String.Empty
            txtRemarkPOBOut.Text = String.Empty
            txtTugRopingOut.Text = String.Empty
            txtRemarkTugRopingOut.Text = String.Empty
            txtLastLine.Text = String.Empty
            txtTimeLastLine.Text = String.Empty
            'txtTimeLastLineFrom.Text = String.Empty
            txtRemarkLastLine.Text = String.Empty
            txtNext.Text = String.Empty
            txtRemarkNext.Text = String.Empty
            txtEnd.Text = String.Empty
            txtTimeEnd.Text = String.Empty
            txtRemarkEnd.Text = String.Empty
            txtAdditionOut.Text = String.Empty

            Dim User_Modify As String = String.Empty
            'lbSIGNATUREOut.Text = String.Empty
            btnSave.Visible = True
            btnCheck.Visible = False
            btnApprove.Visible = False
            btnReject.Visible = False
            btnSend.Visible = True
        End If
        gridTugOut.DataSource = ViewState("Tug")
        gridTugOut.DataBind()
    End Sub

    Private Sub gridItem_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridItem.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Dim imgVPS As Image = CType(e.Row.FindControl("imgVPS"), Image)
            Dim imgMRI As Image = CType(e.Row.FindControl("imgMRI"), Image)
            Dim imgVPSComment As Image = CType(e.Row.FindControl("imgVPSComment"), Image)
            Dim imgMRIComment As Image = CType(e.Row.FindControl("imgMRIComment"), Image)
            Dim imgMKT As Image = CType(e.Row.FindControl("imgMKT"), Image)
            Dim imgProduct As Image = CType(e.Row.FindControl("imgProduct"), Image)
            Dim imgCustom As Image = CType(e.Row.FindControl("imgCustom"), Image)
            Dim imgFinance As Image = CType(e.Row.FindControl("imgFinance"), Image)
            Dim imgApprove As Image = CType(e.Row.FindControl("imgApprove"), Image)
            clsFc.GetImageStatus(imgVPS, imgMRI, imgMKT, gridItem.DataKeys(e.Row.RowIndex).Values("VPS_approve"), gridItem.DataKeys(e.Row.RowIndex).Values("MRI_approve"), gridItem.DataKeys(e.Row.RowIndex).Values("Marketing_approve"), gridItem.DataKeys(e.Row.RowIndex).Values("Status"))
            clsFc.GetImageStatusMKT(imgProduct, imgCustom, imgFinance, gridItem.DataKeys(e.Row.RowIndex).Values("MKT_Product"), gridItem.DataKeys(e.Row.RowIndex).Values("MKT_Custom"), gridItem.DataKeys(e.Row.RowIndex).Values("MKT_Finance"))

            If (gridItem.DataKeys(e.Row.RowIndex).Values("Payin") & "" = "0") Then
                Dim dt As DataTable = clsGet.Get_Booking_Gl_Account(gridItem.DataKeys(e.Row.RowIndex).Values("Booking_no"), gridItem.DataKeys(e.Row.RowIndex).Values("MAgent_no"))
                If (dt.Rows.Count > 0) Then
                    imgMKT.ImageUrl = "~/Image/green.png"
                Else
                    imgMKT.ImageUrl = "~/Image/red.png"
                End If
            ElseIf (gridItem.DataKeys(e.Row.RowIndex).Values("Marketing_approve") & "" = "") Then
                imgMKT.ImageUrl = "~/Image/red.png"
            Else
                imgMKT.ImageUrl = "~/Image/green.png"
            End If

            Dim dtap As DataTable = clsGet.Get_Booking_LogSheet_Out(gridItem.DataKeys(e.Row.RowIndex).Values("Booking_no"))
            If (dtap.Rows.Count > 0) Then
                If (dtap.Rows(0).Item("Status") & "" = "C") Then
                    imgApprove.ImageUrl = "~/Image/blue.png"
                    imgApprove.ToolTip = "Waiting Approve by PTMMSEC"
                ElseIf (dtap.Rows(0).Item("Status") & "" = "A") Then
                    imgApprove.ImageUrl = "~/Image/green.png"
                    imgApprove.ToolTip = "Approve"
                ElseIf (dtap.Rows(0).Item("Status") & "" = "S") Then
                    imgApprove.ImageUrl = "~/Image/Yellow.png"
                    imgApprove.ToolTip = "Waiting Approve by PTMMDAY"
                ElseIf (dtap.Rows(0).Item("Status") & "" = "R") Then
                    imgApprove.ImageUrl = "~/Image/red.png"
                    imgApprove.ToolTip = "During Operation"
                Else
                    imgApprove.ImageUrl = "~/Image/red.png"
                    imgApprove.ToolTip = "During Operation"
                End If
            Else
                imgApprove.ImageUrl = "~/Image/red.png"
                imgApprove.ToolTip = "During Operation"
            End If

            If (gridItem.DataKeys(e.Row.RowIndex).Values("Remark_VPS") & "" <> "") Then
                imgVPSComment.Visible = True
            Else
                imgVPSComment.Visible = False
            End If
            If (gridItem.DataKeys(e.Row.RowIndex).Values("Remark_MRI") & "" <> "") Then
                imgMRIComment.Visible = True
            Else
                imgMRIComment.Visible = False
            End If

            Dim imgConfrim As Image = CType(e.Row.FindControl("imgConfrim"), Image)
            If (gridItem.DataKeys(e.Row.RowIndex).Values("Confirm_Time") & "" = "Y") Then
                imgConfrim.ImageUrl = "~/Image/4558.png"
            Else
                imgConfrim.ImageUrl = "~/Image/4557.gif"
            End If

            Dim Status As String = ""
            Dim Tooltip As String = ""
            clsGet.GetStatus_Working(gridItem.DataKeys(e.Row.RowIndex).Values("Booking_no"), gridItem.DataKeys(e.Row.RowIndex).Values("PORT"), Status, Tooltip)
            e.Row.Cells(2).Text = Status
            e.Row.Cells(2).ToolTip = Tooltip

            If (gridItem.DataKeys(e.Row.RowIndex).Values("Date_From") & "" <> "") Then
                If (gridItem.DataKeys(e.Row.RowIndex).Values("Date_From") & "" = gridItem.DataKeys(e.Row.RowIndex).Values("Date_To") & "") Then
                    e.Row.Cells(4).Text = gridItem.DataKeys(e.Row.RowIndex).Values("Date_To")
                Else
                    e.Row.Cells(4).Text = gridItem.DataKeys(e.Row.RowIndex).Values("Date_From").ToString.Substring(0, 2) & "-" & gridItem.DataKeys(e.Row.RowIndex).Values("Date_To")
                End If

                e.Row.Cells(5).Text = gridItem.DataKeys(e.Row.RowIndex).Values("Booking_Date") & " : " & gridItem.DataKeys(e.Row.RowIndex).Values("Booking_Time")
                e.Row.Cells(6).Text = e.Row.Cells(6).Text & " hours"
                If (gridItem.DataKeys(e.Row.RowIndex).Values("Vehicle_Destination") & "" = "O") Then
                    If (gridItem.DataKeys(e.Row.RowIndex).Values("Vehicle_TypeBuy") & "" = "F") Then
                        e.Row.Cells(18).Text = "FOB" 'รูปแบบการซื้อขาย
                    ElseIf (gridItem.DataKeys(e.Row.RowIndex).Values("Vehicle_TypeBuy") & "" = "C") Then
                        e.Row.Cells(18).Text = "CFR"
                    ElseIf (gridItem.DataKeys(e.Row.RowIndex).Values("Vehicle_TypeBuy") & "" = "I") Then
                        e.Row.Cells(18).Text = "CIF"
                    Else
                        e.Row.Cells(18).Text = "อื่นๆ" 'รูปแบบการซื้อขาย
                    End If
                Else
                    e.Row.Cells(18).Text = "" 'รูปแบบการซื้อขาย
                    e.Row.Cells(17).Text = ""
                End If
                If (gridItem.DataKeys(e.Row.RowIndex).Values("Demurrage_Money") & "" = "B") Then
                    If (gridItem.DataKeys(e.Row.RowIndex).Values("Demurrage") & "" = "") Then
                        e.Row.Cells(17).Text = "- BAHT"
                    Else
                        e.Row.Cells(17).Text = gridItem.DataKeys(e.Row.RowIndex).Values("Demurrage") & " BAHT "
                    End If
                ElseIf (gridItem.DataKeys(e.Row.RowIndex).Values("Demurrage_Money") & "" = "U") Then
                    If (gridItem.DataKeys(e.Row.RowIndex).Values("Demurrage") & "" = "") Then
                        e.Row.Cells(17).Text = "- USD"
                    Else
                        e.Row.Cells(17).Text = gridItem.DataKeys(e.Row.RowIndex).Values("Demurrage") & " USD "
                    End If
                End If
            End If

            Dim Product_Name As String = ""
            Dim Product_Amount As String = ""
            Dim Owner_name As String = ""
            Dim dtLog As DataTable
            dtLog = clsGet.Get_Booking_Product(gridItem.DataKeys(e.Row.RowIndex).Values("Booking_no"))
            For Each dr As DataRow In dtLog.Rows
                If (dr.Item("Product_name") & "" <> "") Then
                    If (Product_Name <> "") Then
                        Product_Name &= "<br />" & dr.Item("Product_name") & ""
                        Product_Amount &= "<br />" & dr.Item("Product_Amount") & ""
                        Owner_name &= "<br />" & dr.Item("Owner_Product_Name") & ""
                    Else
                        Product_Name = dr.Item("Product_name") & ""
                        Product_Amount = dr.Item("Product_Amount") & ""
                        Owner_name = dr.Item("Owner_Product_Name") & ""
                    End If
                End If
            Next
            If (gridItem.DataKeys(e.Row.RowIndex).Values("BunkerFO_Point") & "" <> "") And (gridItem.DataKeys(e.Row.RowIndex).Values("BunkerFO_Point") & "" <> "0") Then
                'BunkerFO 1 คัน,BunkerDO 1 คัน
                If Product_Name = "" Then
                    Product_Name = "BunkerFO"
                    Product_Amount = gridItem.DataKeys(e.Row.RowIndex).Values("BunkerFO_Point") & " คัน"
                Else
                    Product_Name &= "<br />" & "BunkerFO"
                    Product_Amount &= "<br />" & gridItem.DataKeys(e.Row.RowIndex).Values("BunkerFO_Point") & " คัน"
                End If
            End If
            If (gridItem.DataKeys(e.Row.RowIndex).Values("BunkerDO_Point") & "" <> "") And (gridItem.DataKeys(e.Row.RowIndex).Values("BunkerDO_Point") & "" <> "0") Then
                If Product_Name = "" Then
                    Product_Name = "BunkerDO"
                    Product_Amount = gridItem.DataKeys(e.Row.RowIndex).Values("BunkerDO_Point") & " คัน"
                Else
                    Product_Name &= "<br />" & "BunkerDO"
                    Product_Amount &= "<br />" & gridItem.DataKeys(e.Row.RowIndex).Values("BunkerDO_Point") & " คัน"
                End If
            End If
            Dim File_Upload As String = ""
            If (gridItem.DataKeys(e.Row.RowIndex).Values("File_Upload") & "" <> "") Then
                For i As Integer = 0 To gridItem.DataKeys(e.Row.RowIndex).Values("File_Upload").ToString.Split(",").Length - 1
                    If (gridItem.DataKeys(e.Row.RowIndex).Values("File_Upload").ToString.Split(",")(i) & "" <> "") Then
                        If File_Upload = "" Then
                            File_Upload = gridItem.DataKeys(e.Row.RowIndex).Values("File_Upload").ToString.Split(",")(i)
                        Else
                            File_Upload &= "<br />" & gridItem.DataKeys(e.Row.RowIndex).Values("File_Upload").ToString.Split(",")(i)
                        End If
                    End If
                Next
            End If
            e.Row.Cells(7).Text = File_Upload
            e.Row.Cells(8).Text = Product_Name
            e.Row.Cells(9).Text = Product_Amount
            Dim Unit() As String = (gridItem.DataKeys(e.Row.RowIndex).Values("Product_Unit") & "").ToString.Split(",")
            Dim Text As String = ""
            For i As Integer = 0 To Unit.Length - 1
                If (Unit(i) & "" <> "") Then
                    If (Text = "") Then
                        Text = Unit(i)
                    Else
                        Text &= "<br />" & Unit(i)
                    End If
                End If
            Next
            e.Row.Cells(10).Text = Text
            Text = ""
            Unit = (gridItem.DataKeys(e.Row.RowIndex).Values("BUYER") & "").ToString.Split(",")
            For i As Integer = 0 To Unit.Length - 1
                If (Unit(i) & "" <> "") Then
                    If (Text = "") Then
                        Text = Unit(i)
                    Else
                        Text &= "<br />" & Unit(i)
                    End If
                End If
            Next
            e.Row.Cells(11).Text = Text
            Text = ""
            Unit = (gridItem.DataKeys(e.Row.RowIndex).Values("SELLER") & "").ToString.Split(",")
            For i As Integer = 0 To Unit.Length - 1
                If (Unit(i) & "" <> "") Then
                    If (Text = "") Then
                        Text = Unit(i)
                    Else
                        Text &= "<br />" & Unit(i)
                    End If
                End If
            Next
            e.Row.Cells(12).Text = Text
            If (clsFc.GetStatusCancel(gridItem.DataKeys(e.Row.RowIndex).Values("Status"))) Then
                e.Row.BackColor = Drawing.Color.Red
            Else
                Dim Product_Event As String = clsGet.Get_ProductEvent(gridItem.DataKeys(e.Row.RowIndex).Values("Booking_no"))
                If (Product_Event = "L") Then
                    If (gridItem.DataKeys(e.Row.RowIndex).Values("Vehicle_Destination") & "" = "O") Then
                        e.Row.BackColor = Drawing.Color.FromName("#6698FF")
                    Else
                        e.Row.BackColor = Drawing.Color.FromName("#A0CFEC")
                    End If
                    If (gridItem.DataKeys(e.Row.RowIndex).Values("PORT") & "" = "B") Then
                        e.Row.Cells(12).Text = Owner_name
                    End If
                ElseIf (Product_Event = "D") Then
                    If (gridItem.DataKeys(e.Row.RowIndex).Values("Vehicle_Destination") & "" = "O") Then
                        e.Row.BackColor = Drawing.Color.FromName("#347C17")
                    Else
                        e.Row.BackColor = Drawing.Color.FromName("#52D017")
                    End If
                    If (gridItem.DataKeys(e.Row.RowIndex).Values("PORT") & "" = "B") Then
                        e.Row.Cells(11).Text = Owner_name
                    End If
                Else
                    e.Row.BackColor = Drawing.Color.White
                End If
            End If
        ElseIf (e.Row.RowType = DataControlRowType.Pager) Then
            Dim Page As GridViewRow = gridItem.BottomPagerRow
            Dim ddlPage As DropDownList = CType(e.Row.FindControl("ddlPage"), DropDownList)
            Dim lblPageCount As Label = CType(e.Row.FindControl("lbPageCount"), Label)
            If Not (ddlPage Is Nothing) Then
                For i As Integer = 0 To gridItem.PageCount - 1
                    Dim intPageNumber As Integer = i + 1
                    Dim IsItem As New ListItem(intPageNumber.ToString())
                    If (i = gridItem.PageIndex) Then
                        IsItem.Selected = True
                    End If
                    ddlPage.Items.Add(IsItem)
                Next
            End If
            If Not (lblPageCount Is Nothing) Then
                lblPageCount.Text = gridItem.PageCount.ToString()
            End If
        End If
    End Sub

    Private Sub btnBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBack.Click
        LoadBOoking()
        ViewState("Load") = "Not"
        Timer1_Tick(sender, e)
    End Sub

    Private Sub gridItem_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gridItem.RowEditing

    End Sub

    Private Sub TabLogSheet_ActiveTabChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabLogSheet.ActiveTabChanged
        If (TabLogSheet.ActiveTabIndex = 0) Then
            LoadDataVesselLogSheetIn()
        ElseIf (TabLogSheet.ActiveTabIndex = 1) Then
            LoadDataVesselLogSheetOUt()
        End If
    End Sub

    Private Function CreateDataTableTug() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("Tug_Code", GetType(String))
        dt.Columns.Add("Tug_Name", GetType(String))
        dt.Columns.Add("Date_Time", GetType(String))
        dt.Columns.Add("Start_Time", GetType(String))
        dt.Columns.Add("End_Time", GetType(String))
        dt.Columns.Add("Hrs", GetType(String))
        dt.Columns.Add("Remark", GetType(String))
        dt.Columns.Add("Tug_Time", GetType(String))
        dt.Columns.Add("Tug_Event", GetType(String))
        dt.AcceptChanges()
        Return dt
    End Function

    Private Sub btnTug_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTug.Click
        Dim dtTug As DataTable = clsGet.Get_TugBoat(txtTug.Text)
        If (dtTug.Rows.Count = 0) Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ไม่พบข้อมูลเรือทัก ');", True)
            Exit Sub
        End If
        If (txtDate.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุวันที่ ');", True)
            Exit Sub
        End If
        If (txtTimeFrom.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุเวลาเริ่มต้น ');", True)
            Exit Sub
        End If
        If (txtTimeTo.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุเวลาสิ้นสุด ');", True)
            Exit Sub
        End If
        'If (CInt(txtTimeFrom.Text.Substring(0, 2)) > CInt(txtTimeTo.Text.Substring(0, 2))) Then
        '    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ระบุเวลาไม่ถูกต้อง ');", True)
        '    Exit Sub
        'ElseIf (CInt(txtTimeFrom.Text.Substring(0, 2)) = CInt(txtTimeTo.Text.Substring(0, 2))) Then
        '    If (CInt(txtTimeFrom.Text.Substring(3, 2)) > CInt(txtTimeTo.Text.Substring(3, 2))) Then
        '        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ระบุเวลาไม่ถูกต้อง ');", True)
        '        Exit Sub
        '    End If
        'End If
        Dim dt As DataTable = ViewState("Tug")
        Dim Hrs As String = ""
        'If (CInt(txtTimeFrom.Text.Substring(3, 2)) < CInt(txtTimeTo.Text.Substring(3, 2))) Then
        '    Hrs = (CInt(txtTimeTo.Text.Substring(0, 2)) - CInt(txtTimeFrom.Text.Substring(0, 2))).ToString("00") & ":" & (CInt(txtTimeTo.Text.Substring(3, 2)) - CInt(txtTimeFrom.Text.Substring(3, 2))).ToString("00")
        'Else
        '    Dim Hour As Integer = ((CInt(txtTimeTo.Text.Substring(0, 2)) - CInt(txtTimeFrom.Text.Substring(0, 2))) - 1)
        '    Dim Minute As Integer = (60 - CInt(txtTimeFrom.Text.Substring(3, 2))) + CInt(txtTimeTo.Text.Substring(3, 2))
        '    If (Minute = 60) Then
        '        Minute = 0
        '        Hour += 1
        '    End If
        '    Hrs = Hour.ToString("00") & ":" & Minute.ToString("00")
        'End If

        Dim dtStart As String = DateTime.Parse(txtDate.Text & " " & txtTimeFrom.Text)
        Dim dtEnd As String = ""
        If (CInt(txtTimeFrom.Text.Substring(0, 2)) > CInt(txtTimeTo.Text.Substring(0, 2))) Then
            dtEnd = DateTime.Parse(CDate(txtDate.Text).AddDays(1) & " " & txtTimeTo.Text)
        Else
            dtEnd = DateTime.Parse(txtDate.Text & " " & txtTimeTo.Text)
        End If



        Dim ts = DateDiff("n", dtStart, dtEnd) ' in case you need it in minutes

        Hrs = (ts \ 60).ToString("00") & ":" & (ts Mod 60).ToString("00")

        If (ViewState("TugEdit").ToString() = "") Then
            Dim dr As DataRow = dt.NewRow()
            dr.Item("Tug_Code") = dtTug.Rows(0).Item("Tug_Code") & ""
            dr.Item("Tug_Name") = txtTug.Text
            dr.Item("Date_Time") = txtDate.Text
            dr.Item("Start_Time") = txtTimeFrom.Text
            dr.Item("End_Time") = txtTimeTo.Text
            dr.Item("Hrs") = Hrs
            dr.Item("Remark") = txtRemarkTug.Text
            dr.Item("Tug_Time") = txtDate.Text & " / " & txtTimeFrom.Text & "-" & txtTimeTo.Text
            dr.Item("Tug_Event") = ddlEvent.SelectedValue
            dt.Rows.Add(dr)
            dt.AcceptChanges()
        Else
            Dim index As Integer = ViewState("TugEdit")
            dt.Rows(index).Item("Tug_Code") = dtTug.Rows(0).Item("Tug_Code") & ""
            dt.Rows(index).Item("Tug_Name") = txtTug.Text
            dt.Rows(index).Item("Date_Time") = txtDate.Text
            dt.Rows(index).Item("Start_Time") = txtTimeFrom.Text
            dt.Rows(index).Item("End_Time") = txtTimeTo.Text
            dt.Rows(index).Item("Hrs") = Hrs
            dt.Rows(index).Item("Remark") = txtRemarkTug.Text
            dt.Rows(index).Item("Tug_Time") = txtDate.Text & " / " & txtTimeFrom.Text & "-" & txtTimeTo.Text
            dt.Rows(index).Item("Tug_Event") = ddlEvent.SelectedValue
            dt.AcceptChanges()
        End If
        ClearText()
        gridTug.DataSource = dt
        gridTug.DataBind()
        ViewState("Tug") = dt
    End Sub

    Private Sub ClearText()
        txtTug.Text = String.Empty
        txtDate.Text = String.Empty
        txtTimeFrom.Text = String.Empty
        txtTimeTo.Text = String.Empty
        txtRemarkTug.Text = String.Empty
        ViewState("TugEdit") = ""
    End Sub

    Private Sub ClearTextOut()
        txtTugOut.Text = String.Empty
        txtDateOut.Text = String.Empty
        txtTimeFromOut.Text = String.Empty
        txtTimeToOut.Text = String.Empty
        txtRemarkTugOut.Text = String.Empty
        ddlEvent.ClearSelection()
        ddlEventOut.ClearSelection()
        ViewState("TugEdit") = ""
    End Sub

    Private Sub gridTug_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridTug.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If e.CommandName = "cmdedit" Then
            Dim dt As DataTable = ViewState("Tug")
            ViewState("TugEdit") = Index
            txtTug.Text = dt.Rows(Index).Item("Tug_Name")
            txtDate.Text = dt.Rows(Index).Item("Date_Time")
            txtTimeFrom.Text = dt.Rows(Index).Item("Start_Time")
            txtTimeTo.Text = dt.Rows(Index).Item("End_Time")
            txtRemarkTug.Text = dt.Rows(Index).Item("Remark")
            If Not (ddlEvent.Items.FindByValue(dt.Rows(Index).Item("Tug_Event")) Is Nothing) Then
                ddlEvent.SelectedValue = dt.Rows(Index).Item("Tug_Event")
            End If
        ElseIf (e.CommandName = "cmddelete") Then
            Dim dt As DataTable = ViewState("Tug")
            dt.Rows(Index).Delete()
            dt.AcceptChanges()
            gridTug.DataSource = dt
            gridTug.DataBind()
            ViewState("Tug") = dt
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (txtTimeToBerthing.Text <> "") Then
            If (CInt(txtTimeToBerthing.Text.Split(":")(0)) > 23) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ข้อมูลเวลา Berthed ไม่ถูกต้อง');", True)
                Exit Sub
            ElseIf (txtTimeToBerthing.Text.Split(":")(1) > 59) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ข้อมูลเวลา Berthed ไม่ถูกต้อง');", True)
                Exit Sub
            End If
        End If
        If (lbFlag.Text = "TH") Then
            If Not (ViewState("Lay_Time") Is Nothing) Then
                Try
                    If (CInt(ViewState("Lay_Time")) > 0) Then
                        If (txtUpdate.Text.Trim = String.Empty) Then
                            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เรือกำหนดการใช้ Lay Time กรุณาทำการระบุ Anchor up');", True)
                            Exit Sub
                        ElseIf (txtupTime.Text.Trim() = String.Empty) Then
                            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เรือกำหนดการใช้ Lay Time กรุณาทำการระบุ Anchor up');", True)
                            Exit Sub
                        End If
                    End If
                Catch ex As Exception

                End Try
            End If
        End If

        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim dtIn As DataTable = clsGet.Get_Booking_LogSheet_In(ViewState("Booking_no"))
        Dim First As Boolean = True
        If (dtIn.Rows.Count > 0) Then
            First = False
        End If
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Dim User_Modify As String = CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("User_name") & "$ วันที่ : " & DateTime.Now.ToString("dd/MM/yyyy HH:mm")
        Application.Lock()
        conn.Open()
        tran = clsDB.BeginTran(conn)
        clsFc.DeleteBooking_LogSheet(ViewState("Booking_no"), conn, tran)
        If (clsFc.InsertBooking_LogSheet(ViewState("Booking_no"), txtCS.Text, txtDraftFor.Text, txtDraftAFT.Text, txtIMO.Text, conn, tran) < 1) Then
            condition = False
        End If
        clsFc.DeleteBooking_LogSheet_In(ViewState("Booking_no"), conn, tran)
        If (clsFc.InsertBooking_LogSheet_In(ViewState("Booking_no"), txtCommence.Text, txtTimeCommence.Text, txtRemarkCommence.Text, txtLast.Text, txtRemarkLast.Text, txtETA.Text, txtTimeETA.Text, txtRemarkETA.Text, txtStart.Text, txtRemarkStart.Text, txtAnchor.Text, txtTimeAnchor.Text, txtRemarkAnchor.Text, txtPosition.Text, txtRemarkPosition.Text, txtTugRoping.Text, txtTugRopingRemark.Text, txtPilot.Text, txtPOB.Text, txtTimePOB.Text, txtRemarkPOB.Text, txtSchedule.Text, txtTimeSchedule.Text, txtRemarkSchedule.Text, txtBerthing.Text, txtTimeFromBerthing.Text, txtTimeToBerthing.Text, txtRemarkBerthing.Text, txtAddition.Text, User_Modify, txtNor.Text, txtNOrTime.Text, txtUpdate.Text, txtupTime.Text, conn, tran) > 0) Then
            Dim dt As DataTable = ViewState("Tug")
            clsFc.DeleteBooking_LogSheet_Tug(ViewState("Booking_no"), "I", conn, tran)
            For Each dr As DataRow In dt.Rows
                If (clsFc.InsertBooking_LogSheet_Tug(ViewState("Booking_no"), dr.Item("Tug_Code"), dr.Item("Tug_Name"), dr.Item("Date_Time"), dr.Item("Start_Time"), dr.Item("End_Time"), dr.Item("Hrs"), dr.Item("Remark"), "I", dr.Item("Tug_Event"), conn, tran) < 1) Then
                    condition = False
                End If
            Next
        Else
            condition = False
        End If
        If (condition) Then
            clsDB.CommitTran(tran)
            ' LoadBOoking()
            Application.UnLock()
            If (First) Then
                If (txtBerthing.Text.Trim() <> "") Then

                    Try
                        Dim Send As String = ""

                        Dim dtbooking As DataTable = clsGet.Get_BookingOneRecord(ViewState("Booking_no")).Tables(0)
                        Dim str As String = GenBody("เรือได้เข้าสู่ท่าเรือแล้ว", ViewState("Booking_no"), dtbooking.Rows(0).Item("MAgent_no") & "-" & dtbooking.Rows(0).Item("MAgent_name"), dtbooking.Rows(0).Item("Vehicle_no") & "-" & dtbooking.Rows(0).Item("Vehicle_Text"), dtbooking.Rows(0).Item("Date_From") & "-" & dtbooking.Rows(0).Item("Date_To"), txtBerthing.Text & " " & txtTimeFromBerthing.Text & "-" & txtTimeToBerthing.Text, "", "", "")
                        If (dtbooking.Rows(0).Item("PORT") = "L") Then
                            Send = "LCT"
                        Else
                            Send = "BCT"
                        End If
                        SendMail(Send, str, ViewState("Booking_no"), "")
                    Catch ex As Exception

                    End Try
                End If
            End If
            LoadDataVesselLogSheetIn()
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('บันทึกข้อมูลสำเร็จ');", True)
        Else
            clsDB.RollBackTran(tran)
            Application.UnLock()
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึก');", True)
        End If
        conn.Dispose()
    End Sub

    Private Sub gridTugOut_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridTugOut.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If e.CommandName = "cmdedit" Then
            Dim dt As DataTable = ViewState("Tug")
            ViewState("TugEdit") = Index
            txtTugOut.Text = dt.Rows(Index).Item("Tug_Name")
            txtDateOut.Text = dt.Rows(Index).Item("Date_Time")
            txtTimeFromOut.Text = dt.Rows(Index).Item("Start_Time")
            txtTimeToOut.Text = dt.Rows(Index).Item("End_Time")
            txtRemarkTugOut.Text = dt.Rows(Index).Item("Remark")
            If Not (ddlEventOut.Items.FindByValue(dt.Rows(Index).Item("Tug_Event")) Is Nothing) Then
                ddlEventOut.SelectedValue = dt.Rows(Index).Item("Tug_Event")
            End If
        ElseIf (e.CommandName = "cmddelete") Then
            Dim dt As DataTable = ViewState("Tug")
            dt.Rows(Index).Delete()
            dt.AcceptChanges()
            gridTugOut.DataSource = dt
            gridTugOut.DataBind()
            ViewState("Tug") = dt
        End If
    End Sub

    Private Sub btnTugOut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTugOut.Click
        Dim dtTug As DataTable = clsGet.Get_TugBoat(txtTugOut.Text)
        If (dtTug.Rows.Count = 0) Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ไม่พบข้อมูลเรือทัก ');", True)
            Exit Sub
        End If
        If (txtDateOut.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุวันที่ ');", True)
            Exit Sub
        End If
        If (txtTimeFromOut.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุเวลาเริ่มต้น ');", True)
            Exit Sub
        End If
        If (txtTimeToOut.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุเวลาสิ้นสุด ');", True)
            Exit Sub
        End If
        'If (CInt(txtTimeFromOut.Text.Substring(0, 2)) > CInt(txtTimeToOut.Text.Substring(0, 2))) Then
        '    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ระบุเวลาไม่ถูกต้อง ');", True)
        '    Exit Sub
        'ElseIf (CInt(txtTimeFromOut.Text.Substring(0, 2)) = CInt(txtTimeToOut.Text.Substring(0, 2))) Then
        '    If (CInt(txtTimeFromOut.Text.Substring(3, 2)) > CInt(txtTimeToOut.Text.Substring(3, 2))) Then
        '        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ระบุเวลาไม่ถูกต้อง ');", True)
        '        Exit Sub
        '    End If
        'End If
        Dim dt As DataTable = ViewState("Tug")
        Dim Hrs As String = ""
        'If (CInt(txtTimeFromOut.Text.Substring(3, 2)) < CInt(txtTimeToOut.Text.Substring(3, 2))) Then
        '    Hrs = (CInt(txtTimeToOut.Text.Substring(0, 2)) - CInt(txtTimeFromOut.Text.Substring(0, 2))).ToString("00") & ":" & (CInt(txtTimeTo.Text.Substring(3, 2)) - CInt(txtTimeFrom.Text.Substring(3, 2))).ToString("00")
        'Else
        '    Dim Hour As Integer = ((CInt(txtTimeToOut.Text.Substring(0, 2)) - CInt(txtTimeFromOut.Text.Substring(0, 2))) - 1)
        '    Dim Minute As Integer = (60 - CInt(txtTimeFromOut.Text.Substring(3, 2))) + CInt(txtTimeToOut.Text.Substring(3, 2))
        '    If (Minute = 60) Then
        '        Minute = 0
        '        Hour += 1
        '    End If
        '    Hrs = Hour.ToString("00") & ":" & Minute.ToString("00")
        'End If

        Dim dtStart As String = DateTime.Parse(txtDateOut.Text & " " & txtTimeFromOut.Text)
        Dim dtEnd As String = ""
        If (CInt(txtTimeFromOut.Text.Substring(0, 2)) > CInt(txtTimeToOut.Text.Substring(0, 2))) Then
            dtEnd = DateTime.Parse(CDate(txtDateOut.Text).AddDays(1) & " " & txtTimeToOut.Text)
        Else
            dtEnd = DateTime.Parse(txtDateOut.Text & " " & txtTimeToOut.Text)
        End If

        Dim ts = DateDiff("n", dtStart, dtEnd) ' in case you need it in minutes

        Hrs = (ts \ 60).ToString("00") & ":" & (ts Mod 60).ToString("00")
        If (ViewState("TugEdit").ToString() = "") Then
            Dim dr As DataRow = dt.NewRow()
            dr.Item("Tug_Code") = dtTug.Rows(0).Item("Tug_Code") & ""
            dr.Item("Tug_Name") = txtTugOut.Text
            dr.Item("Date_Time") = txtDateOut.Text
            dr.Item("Start_Time") = txtTimeFromOut.Text
            dr.Item("End_Time") = txtTimeToOut.Text
            dr.Item("Hrs") = Hrs
            dr.Item("Remark") = txtRemarkTugOut.Text
            dr.Item("Tug_Time") = txtDateOut.Text & " / " & txtTimeFromOut.Text & "-" & txtTimeToOut.Text
            dr.Item("Tug_Event") = ddlEventOut.SelectedValue
            dt.Rows.Add(dr)
            dt.AcceptChanges()
        Else
            Dim index As Integer = ViewState("TugEdit")
            dt.Rows(index).Item("Tug_Code") = dtTug.Rows(0).Item("Tug_Code") & ""
            dt.Rows(index).Item("Tug_Name") = txtTugOut.Text
            dt.Rows(index).Item("Date_Time") = txtDateOut.Text
            dt.Rows(index).Item("Start_Time") = txtTimeFromOut.Text
            dt.Rows(index).Item("End_Time") = txtTimeToOut.Text
            dt.Rows(index).Item("Hrs") = Hrs
            dt.Rows(index).Item("Remark") = txtRemarkTugOut.Text
            dt.Rows(index).Item("Tug_Time") = txtDateOut.Text & " / " & txtTimeFromOut.Text & "-" & txtTimeToOut.Text
            dt.Rows(index).Item("Tug_Event") = ddlEventOut.SelectedValue
            dt.AcceptChanges()
        End If
        ClearTextOut()
        gridTugOut.DataSource = dt
        gridTugOut.DataBind()
        ViewState("Tug") = dt
    End Sub

    Private Sub btnSaveOut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveOut.Click
        If (txtTimeLastLine.Text <> "") Then
            If (CInt(txtTimeLastLine.Text.Split(":")(0)) > 23) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ข้อมูลเวลา Last Line ไม่ถูกต้อง');", True)
                Exit Sub
            End If
        ElseIf (txtTimeLastLine.Text.Trim() <> "") Then
            If (txtTimeLastLine.Text.Split(":")(1) > 59) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ข้อมูล Last Last ไม่ถูกต้อง');", True)
                Exit Sub
            End If
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ข้อมูล Last Last ไม่ถูกต้อง');", True)
            Exit Sub
        End If

        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Dim User_Modify As String = CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("User_name") & "$ วันที่ : " & DateTime.Now.ToString("dd/MM/yyyy HH:mm")
        Dim dtForST As DataTable = clsGet.Get_Booking_LogSheet_Out(ViewState("Booking_no"))
        Dim Status As String = ""
        If (dtForST.Rows.Count > 0) Then
            Status = dtForST.Rows(0).Item("Status") & ""
        End If
        Application.Lock()
        conn.Open()
        tran = clsDB.BeginTran(conn)

        clsFc.DeleteBooking_LogSheet(ViewState("Booking_no"), conn, tran)
        If (clsFc.InsertBooking_LogSheet(ViewState("Booking_no"), txtCS.Text, txtDraftFor.Text, txtDraftAFT.Text, txtIMO.Text, conn, tran) < 1) Then
            condition = False
        End If
        clsFc.DeleteBooking_LogSheet_Out(ViewState("Booking_no"), conn, tran)
        If (clsFc.InsertBooking_LogSheet_Out(ViewState("Booking_no"), txtPilotOut.Text, txtPOBOut.Text, txtTimePOBOut.Text, txtRemarkPOBOut.Text, txtTugRopingOut.Text, txtRemarkTugRopingOut.Text, txtLastLine.Text, txtTimeLastLine.Text, txtTimeLastLine.Text, txtRemarkLastLine.Text, txtNext.Text, txtRemarkNext.Text, txtEnd.Text, txtTimeEnd.Text, txtRemarkEnd.Text, txtAdditionOut.Text, User_Modify, Status, conn, tran) > 0) Then
            Dim dt As DataTable = ViewState("Tug")
            clsFc.DeleteBooking_LogSheet_Tug(ViewState("Booking_no"), "O", conn, tran)
            For Each dr As DataRow In dt.Rows
                If (clsFc.InsertBooking_LogSheet_Tug(ViewState("Booking_no"), dr.Item("Tug_Code"), dr.Item("Tug_Name"), dr.Item("Date_Time"), dr.Item("Start_Time"), dr.Item("End_Time"), dr.Item("Hrs"), dr.Item("Remark"), "O", dr.Item("Tug_Event"), conn, tran) < 1) Then
                    condition = False
                End If
            Next
            For i As Integer = 0 To gridProduct.Rows.Count - 1
                Dim txtArmConnect As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmConnect"), TextBox)
                Dim txtArmConnectFrom As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmConnectFrom"), TextBox)
                Dim txtArmConnectTo As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmConnectTo"), TextBox)
                Dim txtRemarkArmConnect As TextBox = CType(gridProduct.Rows(i).FindControl("txtRemarkArmConnect"), TextBox)

                Dim txtArmDis As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmDis"), TextBox)
                Dim txtArmDisFrom As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmDisFrom"), TextBox)
                Dim txtArmDisTo As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmDisTo"), TextBox)
                Dim txtRemarkArmDis As TextBox = CType(gridProduct.Rows(i).FindControl("txtRemarkArmDis"), TextBox)

                Dim txtETD As TextBox = CType(gridProduct.Rows(i).FindControl("txtETD"), TextBox)
                Dim txtETDTime As TextBox = CType(gridProduct.Rows(i).FindControl("txtETDTime"), TextBox)
                Dim txtRemarkETD As TextBox = CType(gridProduct.Rows(i).FindControl("txtRemarkETD"), TextBox)
                clsFc.DeleteBooking_LogSheet_Out_Product(ViewState("Booking_no"), gridProduct.DataKeys(i).Values("Product_no"), conn, tran)
                If (clsFc.InsertBooking_LogSheet_Out_Product(ViewState("Booking_no"), gridProduct.DataKeys(i).Values("Product_no"), txtArmConnect.Text, txtArmConnectFrom.Text, txtArmConnectTo.Text, txtRemarkArmConnect.Text, txtArmDis.Text, txtArmDisFrom.Text, txtArmDisTo.Text, txtRemarkArmDis.Text, txtETD.Text, txtETDTime.Text, txtRemarkETD.Text, conn, tran) < 1) Then
                    condition = False
                End If
            Next
        Else
            condition = False
        End If
        If (condition) Then
            clsDB.CommitTran(tran)
            Application.UnLock()
            'LoadBOoking()
            LoadDataVesselLogSheetOUt()
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('บันทึกข้อมูลสำเร็จ');", True)
        Else
            clsDB.RollBackTran(tran)
            Application.UnLock()
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึก');", True)
        End If
        conn.Dispose()
    End Sub

    Private Sub gridItem_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridItem.RowCreated
        If e.Row.RowType = DataControlRowType.Header Then
            Dim Grid As GridView = CType(sender, GridView)
            e.Row.Cells.Clear()
            Dim GridRow As GridViewRow = New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert)
            Dim GridRow1 As GridViewRow = New GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert)

            Dim table As TableCell = New TableCell()
            table.Text = "BOOKING NO."
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.CssClass = "locked"
            table.Wrap = True
            GridRow1.Cells.Add(table)


            table = New TableCell()
            table.Text = "VESSEL NAME"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.CssClass = "locked"
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "STATUS"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "STATUS APPROVE"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "LAY CAN/LAY DAY"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "REQUIRE DATE/TIME"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "LAY TIME"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "ATTACHMENT"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "COMMODITY"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "QUANTITY"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "UNIT"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "BUYER"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)


            table = New TableCell()
            table.Text = "SELLER"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "APPROVE BY MKT"
            table.HorizontalAlign = HorizontalAlign.Center
            table.ColumnSpan = 5
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "APPROVE BY"
            table.HorizontalAlign = HorizontalAlign.Center
            table.ColumnSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "TARIFF"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "AGENT"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "WHARF"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "PLAN BERTH DATE"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "PLAN BERTH TIME"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "PLAN DEPART DATE"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "PLAN DEPART TIME"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "CONFIRM PLAN BERTH"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "REMAIN LAYTIME(ESTIMATE)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "REMARK"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = ""
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            Grid.Controls(0).Controls.AddAt(0, GridRow1)

            table = New TableCell()
            table.Text = "PRODUCT"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "CUSTOM"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "FINANCE"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "DEMURRAGE"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "TERM"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "PTMP"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "PTMI"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            Grid.Controls(0).Controls.AddAt(1, GridRow)
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If (ViewState("Load") & "" <> "Not") Then
            LoadBOoking()
        Else
            ViewState("Load") = ""
        End If

        For i As Integer = 0 To gridItem.Rows.Count - 1
            Dim dsBooking As DataSet = clsGet.Get_BookingOneRecord(gridItem.DataKeys(i).Values("Booking_no"))
            If (dsBooking.Tables(0).Rows(0).Item("Lay_Time") & "" <> "0") Then
                Dim dtLogIn As DataTable = clsGet.Get_Booking_LogSheet_In(gridItem.DataKeys(i).Values("Booking_no"))
                If Not (dtLogIn.Rows.Count = 0) Then
                    If Not (dtLogIn.Rows(0).Item("Berthing_Date") & "" = "") Or (dtLogIn.Rows(0).Item("Berthing_TimeTo") & "" = "") Then
                        If Not (dtLogIn.Rows(0).Item("POB_Date") & "" = "") Or (dtLogIn.Rows(0).Item("POB_Time") & "" = "") Then
                            Dim dtProduct As DataTable = clsGet.Get_Booking_Product(gridItem.DataKeys(i).Values("Booking_no"))
                            If Not (dtProduct.Rows.Count = 0) Then
                                If (dsBooking.Tables(0).Rows(0).Item("PORT") & "" = "L") Then
                                    Dim Product_no As String = ""
                                    Try
                                        Dim sort As Integer = 0
                                        For Each dr As DataRow In dtProduct.Rows
                                            If (CInt(dr.Item("Product_Index") & "") > sort) Then
                                                sort = CInt(dr.Item("Product_Index"))
                                                Product_no = dr.Item("Product_no")
                                            End If
                                        Next
                                    Catch ex As Exception

                                    End Try
                                    Dim dtLoad As DataTable = clsGet.Get_Load_Unload_Vessel(gridItem.DataKeys(i).Values("Booking_no"))
                                    Dim NOR As String = ""
                                    Dim NOR_Time As String = ""
                                    Dim POB As String = ""
                                    Dim POB_Time As String = ""
                                    If (dtLoad.Rows.Count > 0) Then
                                        If (dtLoad.Rows(0).Item("NOR_Date") & "" <> "") Then
                                            NOR = dtLoad.Rows(0).Item("NOR_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dtLoad.Rows(0).Item("NOR_Date").ToString.Substring(3, 2)) & " 20" & dtLoad.Rows(0).Item("NOR_Date").ToString.Substring(6, 2)
                                            NOR_Time = dtLoad.Rows(0).Item("NOR_Time") & ""
                                        Else
                                            NOR = dtLogIn.Rows(0).Item("NOR_Date") & ""
                                            NOR_Time = dtLogIn.Rows(0).Item("NOR_Time") & ""
                                        End If
                                    Else
                                        NOR = dtLogIn.Rows(0).Item("NOR_Date") & ""
                                        NOR_Time = dtLogIn.Rows(0).Item("NOR_Time") & ""
                                    End If
                                    If (dtLogIn.Rows(0).Item("POB_Date") & "" <> "") Then
                                        If (dtLogIn.Rows(0).Item("Up_Date") & "" <> "") Then
                                            If (CDate(dtLogIn.Rows(0).Item("POB_Date")) < CDate(dtLogIn.Rows(0).Item("Up_Date"))) Then
                                                POB = dtLogIn.Rows(0).Item("Up_Date") & ""
                                                POB_Time = dtLogIn.Rows(0).Item("Up_Time") & ""
                                            ElseIf (CDate(dtLogIn.Rows(0).Item("POB_Date")) = CDate(dtLogIn.Rows(0).Item("Up_Date"))) Then
                                                If (CInt(dtLogIn.Rows(0).Item("POB_Time").ToString().Split(":")(0)) < CInt(dtLogIn.Rows(0).Item("Up_Time").ToString().Split(":")(0))) Then
                                                    POB = dtLogIn.Rows(0).Item("Up_Date") & ""
                                                    POB_Time = dtLogIn.Rows(0).Item("Up_Time") & ""
                                                ElseIf (CInt(dtLogIn.Rows(0).Item("POB_Time").ToString().Split(":")(0)) = CInt(dtLogIn.Rows(0).Item("Up_Time").ToString().Split(":")(0))) Then
                                                    If (CInt(dtLogIn.Rows(0).Item("POB_Time").ToString().Split(":")(1)) < CInt(dtLogIn.Rows(0).Item("Up_Time").ToString().Split(":")(1))) Then
                                                        POB = dtLogIn.Rows(0).Item("Up_Date") & ""
                                                        POB_Time = dtLogIn.Rows(0).Item("Up_Time") & ""
                                                    Else
                                                        POB = dtLogIn.Rows(0).Item("POB_Date") & ""
                                                        POB_Time = dtLogIn.Rows(0).Item("POB_Time") & ""
                                                    End If
                                                Else
                                                    POB = dtLogIn.Rows(0).Item("POB_Date") & ""
                                                    POB_Time = dtLogIn.Rows(0).Item("POB_Time") & ""
                                                End If
                                            Else
                                                POB = dtLogIn.Rows(0).Item("POB_Date") & ""
                                                POB_Time = dtLogIn.Rows(0).Item("POB_Time") & ""
                                            End If
                                        Else
                                            POB = dtLogIn.Rows(0).Item("POB_Date") & ""
                                            POB_Time = dtLogIn.Rows(0).Item("POB_Time") & ""
                                        End If
                                    Else
                                        POB = dtLogIn.Rows(0).Item("Up_Date") & ""
                                        POB_Time = dtLogIn.Rows(0).Item("Up_Time") & ""
                                    End If

                                    dtLoad = clsGet.Get_Load_Unload_Product(gridItem.DataKeys(i).Values("Booking_no"), Product_no)
                                    Dim Dis_Date As String = ""
                                    Dim Dis_Time As String = ""
                                    For Each dl As DataRow In dtLoad.Rows
                                        Try
                                            If (Dis_Date = "") Then
                                                Dis_Date = dl.Item("LU_Disconnect_Date") & ""
                                                Dis_Time = dl.Item("LU_Disconnect_To") & ""
                                            End If
                                            Dim _DD As String = Dis_Date.Substring(0, 2) & " " & GetNameMonth(Dis_Date.Substring(3, 2)) & " " & CInt("20" & Dis_Date.Substring(6, 2))
                                            Dim _D2 As String = dl.Item("LU_Disconnect_Date").Substring(0, 2) & " " & GetNameMonth(dl.Item("LU_Disconnect_Date").Substring(3, 2)) & " " & CInt("20" & dl.Item("LU_Disconnect_Date").Substring(6, 2))
                                            If (CDate(_DD) < CDate(_D2)) Then
                                                Dis_Date = dl.Item("LU_Disconnect_Date") & ""
                                                Dis_Time = dl.Item("LU_Disconnect_To") & ""
                                            ElseIf (CDate(_DD) = CDate(_D2)) Then
                                                If (CInt(Dis_Time.Split(":")(0)) < CInt(dl.Item("LU_Disconnect_To").ToString.Split(":")(0))) Then
                                                    Dis_Date = dl.Item("LU_Disconnect_Date") & ""
                                                    Dis_Time = dl.Item("LU_Disconnect_To") & ""
                                                ElseIf (CInt(Dis_Time.Split(":")(0)) = CInt(dl.Item("LU_Disconnect_To").ToString.Split(":")(0))) Then
                                                    Try
                                                        If (CInt(Dis_Time.Split(":")(1)) < CInt(dl.Item("LU_Disconnect_To").ToString.Split(":")(1))) Then
                                                            Dis_Date = dl.Item("LU_Disconnect_Date") & ""
                                                            Dis_Time = dl.Item("LU_Disconnect_To") & ""
                                                        End If
                                                    Catch ex As Exception

                                                    End Try
                                                End If
                                            End If
                                        Catch ex As Exception

                                        End Try

                                    Next
                                    Dim Berthing_Date As String = dtLogIn.Rows(0).Item("Berthing_Date")
                                    Try
                                        Try
                                            If (CInt(dtLogIn.Rows(0).Item("Berthing_TimeTo").ToString().Split(":")(0)) < CInt(dtLogIn.Rows(0).Item("Berthing_TimeFrom").ToString.Split(":")(0))) Then
                                                Berthing_Date = CDate(Berthing_Date).AddDays(1).ToString("dd MMM", New CultureInfo("en-US")) & " " & Berthing_Date.Split(" ")(2)
                                            End If
                                        Catch ex As Exception

                                        End Try
                                    Catch ex As Exception

                                    End Try

                                    If (dtLoad.Rows.Count > 0) Then
                                        gridItem.Rows(i).Cells(28).Text = clsGet.ComputeRemainLaytime(Berthing_Date, dtLogIn.Rows(0).Item("Berthing_TimeTo"), dsBooking.Tables(0).Rows(0).Item("Lay_Time"), dsBooking.Tables(0).Rows(0).Item("Date_From"), dsBooking.Tables(0).Rows(0).Item("Date_To"), POB, POB_Time, Dis_Date & "", Dis_Time & "", NOR, NOR_Time)
                                    Else
                                        gridItem.Rows(i).Cells(28).Text = clsGet.ComputeRemainLaytime(Berthing_Date, dtLogIn.Rows(0).Item("Berthing_TimeTo"), dsBooking.Tables(0).Rows(0).Item("Lay_Time"), dsBooking.Tables(0).Rows(0).Item("Date_From"), dsBooking.Tables(0).Rows(0).Item("Date_To"), POB, POB_Time, "", "", NOR, NOR_Time)
                                    End If

                                Else
                                    Dim dtProductBulk As DataTable = clsGet.Get_Booking_Product(gridItem.DataKeys(i).Values("Booking_no"))
                                    If (dtProductBulk.Rows.Count > 0) Then
                                        Dim chkCoil As Boolean = False
                                        For Each dx As DataRow In dtProductBulk.Rows
                                            If (dx.Item("Group_no") & "" = "00007") Then
                                                chkCoil = True
                                                Exit For
                                            End If
                                        Next
                                        If (chkCoil) Then
                                            gridItem.Rows(i).Cells(28).Text = clsGet.ComputeRemainLaytimeBulk(dsBooking.Tables(0).Rows(0).Item("Lay_Time"), gridItem.DataKeys(i).Values("Booking_no"))
                                        End If
                                    End If
                                End If
                                If (gridItem.Rows(i).Cells(28).Text <> "") Then
                                    If (gridItem.Rows(i).Cells(28).Text.Substring(0, 1) = "-") Then
                                        gridItem.Rows(i).Cells(28).ForeColor = Drawing.Color.Red
                                    Else
                                        gridItem.Rows(i).Cells(28).ForeColor = Drawing.Color.Black
                                    End If
                                End If

                            End If
                        End If
                    End If
                End If
            End If
        Next
    End Sub

    Private Function GetNameMonth(ByVal Month As String) As String
        Try
            Select Case (CInt(Month).ToString("00"))
                Case "01"
                    Return "Jan"
                Case "02"
                    Return "Feb"
                Case "03"
                    Return "Mar"
                Case "04"
                    Return "Apr"
                Case "05"
                    Return "May"
                Case "06"
                    Return "Jun"
                Case "07"
                    Return "Jul"
                Case "08"
                    Return "Aug"
                Case "09"
                    Return "Sep"
                Case "10"
                    Return "Oct"
                Case "11"
                    Return "Nov"
                Case "12"
                    Return "Dec"
                Case Else
                    Return ""
            End Select
        Catch ex As Exception
            Return Month
        End Try

    End Function

    Private Sub btnReLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReLoad.Click
        gridItem.DataSource = ViewState("LogSheet")
        gridItem.DataBind()
        LoadBOoking()
        ViewState("Load") = "Not"
        Timer1_Tick(sender, e)
    End Sub

    Private Sub btnBackOut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBackOut.Click
        LoadBOoking()
        ViewState("Load") = "Not"
        Timer1_Tick(sender, e)
    End Sub

    Private Sub gridProduct_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridProduct.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Dim lbCommence As Label = CType(e.Row.FindControl("lbCommence"), Label)
            Dim lbComplete As Label = CType(e.Row.FindControl("lbComplete"), Label)

            Dim lbETC As Label = CType(e.Row.FindControl("lbETC"), Label)
            lbCommence.Text = gridProduct.DataKeys(e.Row.RowIndex).Values("LU_StartActual_Date") & " " & gridProduct.DataKeys(e.Row.RowIndex).Values("LU_StartActual_Time")
            lbComplete.Text = gridProduct.DataKeys(e.Row.RowIndex).Values("LU_StopActual_Date") & " " & gridProduct.DataKeys(e.Row.RowIndex).Values("LU_StopActual_Time")
            lbETC.Text = gridProduct.DataKeys(e.Row.RowIndex).Values("LU_StopStandard_Date") & " " & gridProduct.DataKeys(e.Row.RowIndex).Values("LU_StopStandard_Time")

            Dim dt As DataTable = clsGet.Get_Booking_Product_LogSheetOut(gridProduct.DataKeys(e.Row.RowIndex).Values("Booking_no") & "")
            If (dt.Rows.Count > 0) Then
                Dim txtArmConnect As TextBox = CType(e.Row.FindControl("txtArmConnect"), TextBox)
                Dim txtArmConnectFrom As TextBox = CType(e.Row.FindControl("txtArmConnectFrom"), TextBox)
                Dim txtArmConnectTo As TextBox = CType(e.Row.FindControl("txtArmConnectTo"), TextBox)
                Dim txtRemarkArmConnect As TextBox = CType(e.Row.FindControl("txtRemarkArmConnect"), TextBox)

                Dim txtArmDis As TextBox = CType(e.Row.FindControl("txtArmDis"), TextBox)
                Dim txtArmDisFrom As TextBox = CType(e.Row.FindControl("txtArmDisFrom"), TextBox)
                Dim txtArmDisTo As TextBox = CType(e.Row.FindControl("txtArmDisTo"), TextBox)
                Dim txtRemarkArmDis As TextBox = CType(e.Row.FindControl("txtRemarkArmDis"), TextBox)

                Dim txtETD As TextBox = CType(e.Row.FindControl("txtETD"), TextBox)
                Dim txtETDTime As TextBox = CType(e.Row.FindControl("txtETDTime"), TextBox)
                Dim txtRemarkETD As TextBox = CType(e.Row.FindControl("txtRemarkETD"), TextBox)

                txtArmConnect.Text = dt.Rows(0).Item("Connect_Date") & ""
                txtArmConnectFrom.Text = dt.Rows(0).Item("Connect_TimeFrom") & ""
                txtArmConnectTo.Text = dt.Rows(0).Item("Connect_TimeTo") & ""
                txtRemarkArmConnect.Text = dt.Rows(0).Item("Connect_Remark") & ""

                txtArmDis.Text = dt.Rows(0).Item("Dis_Date") & ""
                txtArmDisFrom.Text = dt.Rows(0).Item("Dis_TimeFrom") & ""
                txtArmDisTo.Text = dt.Rows(0).Item("Dis_TimeTo") & ""
                txtRemarkArmDis.Text = dt.Rows(0).Item("Dis_Remark") & ""

                txtETD.Text = dt.Rows(0).Item("ETD_Date") & ""
                txtETDTime.Text = dt.Rows(0).Item("ETD_Time") & ""
                txtRemarkETD.Text = dt.Rows(0).Item("ETD_Remark") & ""
            Else
                Dim txtArmConnect As TextBox = CType(e.Row.FindControl("txtArmConnect"), TextBox)
                Dim txtArmConnectFrom As TextBox = CType(e.Row.FindControl("txtArmConnectFrom"), TextBox)
                Dim txtArmConnectTo As TextBox = CType(e.Row.FindControl("txtArmConnectTo"), TextBox)
                Dim txtArmDis As TextBox = CType(e.Row.FindControl("txtArmDis"), TextBox)
                Dim txtArmDisFrom As TextBox = CType(e.Row.FindControl("txtArmDisFrom"), TextBox)
                Dim txtArmDisTo As TextBox = CType(e.Row.FindControl("txtArmDisTo"), TextBox)
                txtArmConnect.Text = gridProduct.DataKeys(e.Row.RowIndex).Values("LU_Connect_Date") & ""
                txtArmConnectFrom.Text = gridProduct.DataKeys(e.Row.RowIndex).Values("LU_Connect_From") & ""
                txtArmConnectTo.Text = gridProduct.DataKeys(e.Row.RowIndex).Values("LU_Connect_To") & ""
                txtArmDis.Text = gridProduct.DataKeys(e.Row.RowIndex).Values("LU_Disconnect_Date") & ""
                txtArmDisFrom.Text = gridProduct.DataKeys(e.Row.RowIndex).Values("LU_Disconnect_From") & ""
                txtArmDisTo.Text = gridProduct.DataKeys(e.Row.RowIndex).Values("LU_Disconnect_To") & ""
            End If
        End If
    End Sub

    Private Sub gridTug_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridTug.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Select Case (gridTug.DataKeys(e.Row.RowIndex).Values("Tug_Event") & "")
                Case "W"
                    e.Row.Cells(3).Text = "Working"
                Case "S"
                    e.Row.Cells(3).Text = "Stand By"
                Case "P"
                    e.Row.Cells(3).Text = "Pilot Transport"
                Case "O"
                    e.Row.Cells(3).Text = "Other"
                Case "M"
                    e.Row.Cells(3).Text = "Mooring"
                Case Else
                    e.Row.Cells(3).Text = ""
            End Select
        End If
    End Sub

    Private Sub gridTugOut_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridTugOut.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Select Case (gridTugOut.DataKeys(e.Row.RowIndex).Values("Tug_Event") & "")
                Case "W"
                    e.Row.Cells(3).Text = "Working"
                Case "S"
                    e.Row.Cells(3).Text = "Stand By"
                Case "P"
                    e.Row.Cells(3).Text = "Pilot Transport"
                Case "O"
                    e.Row.Cells(3).Text = "Other"
                Case "M"
                    e.Row.Cells(3).Text = "Mooring"
                Case Else
                    e.Row.Cells(3).Text = ""
            End Select
        End If
    End Sub

    'Private Sub btnComplete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComplete.Click
    '  If (txtTimeLastLine.Text.Trim = "") Then
    '        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุข้อมูล Last Line');", True)
    '        Exit Sub
    '    ElseIf (txtTimeLastLine.Text.Split(":")(0) > 23) Then
    '        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ข้อมูล Last Last ไม่ถูกต้อง');", True)
    '        Exit Sub
    '    ElseIf (txtTimeLastLine.Text.Split(":")(1) > 59) Then
    '        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ข้อมูล Last Last ไม่ถูกต้อง');", True)
    '        Exit Sub
    '    End If
    '    Dim conn As New System.Data.SqlClient.SqlConnection(connString)
    '    Dim clsDB As New ClassDatabase
    '    Dim clsFc As New ClassFunction
    '    Dim condition As Boolean = True
    '    Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
    '    Dim User_Modify As String = CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("User_name") & "$ วันที่ : " & DateTime.Now.ToString("dd/MM/yyyy HH:mm")
    '    Application.Lock()
    '    conn.Open()
    '    tran = clsDB.BeginTran(conn)

    '    clsFc.DeleteBooking_LogSheet(ViewState("Booking_no"), conn, tran)
    '    If (clsFc.InsertBooking_LogSheet(ViewState("Booking_no"), txtCS.Text, txtDraftFor.Text, txtDraftAFT.Text, txtIMO.Text, conn, tran) < 1) Then
    '        condition = False
    '    End If
    '    clsFc.DeleteBooking_LogSheet_Out(ViewState("Booking_no"), conn, tran)
    '    If (clsFc.InsertBooking_LogSheet_Out(ViewState("Booking_no"), txtPilotOut.Text, txtPOBOut.Text, txtTimePOBOut.Text, txtRemarkPOBOut.Text, txtTugRopingOut.Text, txtRemarkTugRopingOut.Text, txtLastLine.Text, txtTimeLastLine.Text, txtRemarkLastLine.Text, txtNext.Text, txtRemarkNext.Text, txtEnd.Text, txtTimeEnd.Text, txtRemarkEnd.Text, txtAdditionOut.Text, User_Modify, conn, tran) > 0) Then
    '        Dim dt As DataTable = ViewState("Tug")
    '        clsFc.DeleteBooking_LogSheet_Tug(ViewState("Booking_no"), "O", conn, tran)
    '        For Each dr As DataRow In dt.Rows
    '            If (clsFc.InsertBooking_LogSheet_Tug(ViewState("Booking_no"), dr.Item("Tug_Code"), dr.Item("Tug_Name"), dr.Item("Date_Time"), dr.Item("Start_Time"), dr.Item("End_Time"), dr.Item("Hrs"), dr.Item("Remark"), "O", dr.Item("Tug_Event"), conn, tran) < 1) Then
    '                condition = False
    '            End If
    '        Next
    '        For i As Integer = 0 To gridProduct.Rows.Count - 1
    '            Dim txtArmConnect As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmConnect"), TextBox)
    '            Dim txtArmConnectFrom As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmConnectFrom"), TextBox)
    '            Dim txtArmConnectTo As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmConnectTo"), TextBox)
    '            Dim txtRemarkArmConnect As TextBox = CType(gridProduct.Rows(i).FindControl("txtRemarkArmConnect"), TextBox)

    '            Dim txtArmDis As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmDis"), TextBox)
    '            Dim txtArmDisFrom As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmDisFrom"), TextBox)
    '            Dim txtArmDisTo As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmDisTo"), TextBox)
    '            Dim txtRemarkArmDis As TextBox = CType(gridProduct.Rows(i).FindControl("txtRemarkArmDis"), TextBox)

    '            Dim txtETD As TextBox = CType(gridProduct.Rows(i).FindControl("txtETD"), TextBox)
    '            Dim txtETDTime As TextBox = CType(gridProduct.Rows(i).FindControl("txtETDTime"), TextBox)
    '            Dim txtRemarkETD As TextBox = CType(gridProduct.Rows(i).FindControl("txtRemarkETD"), TextBox)
    '            clsFc.DeleteBooking_LogSheet_Out_Product(ViewState("Booking_no"), gridProduct.DataKeys(i).Values("Product_no"), conn, tran)
    '            If (clsFc.InsertBooking_LogSheet_Out_Product(ViewState("Booking_no"), gridProduct.DataKeys(i).Values("Product_no"), txtArmConnect.Text, txtArmConnectFrom.Text, txtArmConnectTo.Text, txtRemarkArmConnect.Text, txtArmDis.Text, txtArmDisFrom.Text, txtArmDisTo.Text, txtRemarkArmDis.Text, txtETD.Text, txtETDTime.Text, txtRemarkETD.Text, conn, tran) < 1) Then
    '                condition = False
    '            End If
    '        Next
    '    Else
    '        condition = False
    '    End If

    '    If (clsFc.UpdateBookingStatusOneField(ViewState("Booking_no"), "OA", conn, tran) < 1) Then
    '        condition = False
    '    End If
    '    If (condition) Then
    '        clsDB.CommitTran(tran)
    '        'LoadBOoking()
    '        LoadDataVesselLogSheetOUt()
    '        btnSave.Visible = False
    '        btnSaveOut.Visible = False
    '        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('บันทึกข้อมูลสำเร็จ');", True)
    '    Else
    '        clsDB.RollBackTran(tran)
    '        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึก');", True)
    '    End If
    '    Application.UnLock()
    '    conn.Dispose()
    'End Sub

    Private Sub btnCheck_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCheck.Click
        'If (txtTimeLastLineFrom.Text.Trim = "") Then
        '    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุข้อมูล Last Line');", True)
        '    Exit Sub
        'ElseIf (txtTimeLastLineFrom.Text.Split(":")(0) > 23) Then
        '    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ข้อมูล Last Last ไม่ถูกต้อง');", True)
        '    Exit Sub
        'ElseIf (txtTimeLastLineFrom.Text.Split(":")(1) > 59) Then
        '    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ข้อมูล Last Last ไม่ถูกต้อง');", True)
        '    Exit Sub
        'End If
        If (txtTimeLastLine.Text.Trim = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุข้อมูล Last Line');", True)
            Exit Sub
        ElseIf (txtTimeLastLine.Text.Split(":")(0) > 23) Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ข้อมูล Last Last ไม่ถูกต้อง');", True)
            Exit Sub
        ElseIf (txtTimeLastLine.Text.Split(":")(1) > 59) Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ข้อมูล Last Last ไม่ถูกต้อง');", True)
            Exit Sub
        End If
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Dim User_Modify As String = CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("User_name")
        Application.Lock()
        conn.Open()
        Dim A_Code As String = clsDB.GeneratorID(conn, "Action_Operation")
        tran = clsDB.BeginTran(conn)
        clsFc.DeleteBooking_LogSheet(ViewState("Booking_no"), conn, tran)
        If (clsFc.InsertBooking_LogSheet(ViewState("Booking_no"), txtCS.Text, txtDraftFor.Text, txtDraftAFT.Text, txtIMO.Text, conn, tran) < 1) Then
            condition = False
        End If
        clsFc.DeleteBooking_LogSheet_Out(ViewState("Booking_no"), conn, tran)
        If (clsFc.InsertBooking_LogSheet_Out(ViewState("Booking_no"), txtPilotOut.Text, txtPOBOut.Text, txtTimePOBOut.Text, txtRemarkPOBOut.Text, txtTugRopingOut.Text, txtRemarkTugRopingOut.Text, txtLastLine.Text, txtTimeLastLine.Text, txtTimeLastLine.Text, txtRemarkLastLine.Text, txtNext.Text, txtRemarkNext.Text, txtEnd.Text, txtTimeEnd.Text, txtRemarkEnd.Text, txtAdditionOut.Text, User_Modify, "C", conn, tran) > 0) Then
            Dim dt As DataTable = ViewState("Tug")
            clsFc.DeleteBooking_LogSheet_Tug(ViewState("Booking_no"), "O", conn, tran)
            For Each dr As DataRow In dt.Rows
                If (clsFc.InsertBooking_LogSheet_Tug(ViewState("Booking_no"), dr.Item("Tug_Code"), dr.Item("Tug_Name"), dr.Item("Date_Time"), dr.Item("Start_Time"), dr.Item("End_Time"), dr.Item("Hrs"), dr.Item("Remark"), "O", dr.Item("Tug_Event"), conn, tran) < 1) Then
                    condition = False
                End If
            Next
            For i As Integer = 0 To gridProduct.Rows.Count - 1
                Dim txtArmConnect As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmConnect"), TextBox)
                Dim txtArmConnectFrom As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmConnectFrom"), TextBox)
                Dim txtArmConnectTo As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmConnectTo"), TextBox)
                Dim txtRemarkArmConnect As TextBox = CType(gridProduct.Rows(i).FindControl("txtRemarkArmConnect"), TextBox)

                Dim txtArmDis As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmDis"), TextBox)
                Dim txtArmDisFrom As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmDisFrom"), TextBox)
                Dim txtArmDisTo As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmDisTo"), TextBox)
                Dim txtRemarkArmDis As TextBox = CType(gridProduct.Rows(i).FindControl("txtRemarkArmDis"), TextBox)

                Dim txtETD As TextBox = CType(gridProduct.Rows(i).FindControl("txtETD"), TextBox)
                Dim txtETDTime As TextBox = CType(gridProduct.Rows(i).FindControl("txtETDTime"), TextBox)
                Dim txtRemarkETD As TextBox = CType(gridProduct.Rows(i).FindControl("txtRemarkETD"), TextBox)
                clsFc.DeleteBooking_LogSheet_Out_Product(ViewState("Booking_no"), gridProduct.DataKeys(i).Values("Product_no"), conn, tran)
                If (clsFc.InsertBooking_LogSheet_Out_Product(ViewState("Booking_no"), gridProduct.DataKeys(i).Values("Product_no"), txtArmConnect.Text, txtArmConnectFrom.Text, txtArmConnectTo.Text, txtRemarkArmConnect.Text, txtArmDis.Text, txtArmDisFrom.Text, txtArmDisTo.Text, txtRemarkArmDis.Text, txtETD.Text, txtETDTime.Text, txtRemarkETD.Text, conn, tran) < 1) Then
                    condition = False
                End If
            Next
        Else
            condition = False
        End If
        'If (clsFc.UPDATEBooking_LogSheet_OutStatus(ViewState("Booking_no"), "C", conn, tran) < 1) Then
        '    condition = False
        'End If
        If (clsFc.InsertAction_Operation(A_Code, ViewState("Booking_no"), "Check Data By", DateTime.Now.ToString("dd MMMM yyyy", New CultureInfo("en-US")), DateTime.Now.ToString("HH:mm"), CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Name"), CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("User_name"), "", conn, tran) < 1) Then
            condition = False
        End If
        If (condition) Then
            clsDB.CommitTran(tran)
            LoadDataVesselLogSheetOUt()
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('บันทึกการตรวจสอบข้อมูลสำเร็จ');", True)
        Else
            clsDB.RollBackTran(tran)
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึก');", True)
        End If
        Application.UnLock()
        conn.Dispose()
    End Sub

    Private Sub btnApprove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApprove.Click
        'If (txtTimeLastLineFrom.Text.Trim = "") Then
        '    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุข้อมูล Last Line');", True)
        '    Exit Sub
        'ElseIf (txtTimeLastLineFrom.Text.Split(":")(0) > 23) Then
        '    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ข้อมูล Last Last ไม่ถูกต้อง');", True)
        '    Exit Sub
        'ElseIf (txtTimeLastLineFrom.Text.Split(":")(1) > 59) Then
        '    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ข้อมูล Last Last ไม่ถูกต้อง');", True)
        '    Exit Sub
        'End If
        If (txtTimeLastLine.Text.Trim = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุข้อมูล Last Line');", True)
            Exit Sub
        ElseIf (txtTimeLastLine.Text.Split(":")(0) > 23) Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ข้อมูล Last Last ไม่ถูกต้อง');", True)
            Exit Sub
        ElseIf (txtTimeLastLine.Text.Split(":")(1) > 59) Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ข้อมูล Last Last ไม่ถูกต้อง');", True)
            Exit Sub
        End If
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Dim User_Modify As String = CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("User_name")
        Dim dtLogSheet As DataTable = clsGet.Get_Load_Unload_Vessel(ViewState("Booking_no"))
        Dim dtLogIn As DataTable = clsGet.Get_Booking_LogSheet_In(ViewState("Booking_no"))
        Dim Berth As String
        If (dtLogIn.Rows.Count > 0) Then
            Berth = dtLogIn.Rows(0).Item("Berthing_Date") & " " & dtLogIn.Rows(0).Item("Berthing_TimeFrom") & "-" & dtLogIn.Rows(0).Item("Berthing_TimeTo")
        End If
        Application.Lock()
        conn.Open()
        Dim A_Code As String = clsDB.GeneratorID(conn, "Action_Operation")
        tran = clsDB.BeginTran(conn)
        clsFc.DeleteBooking_LogSheet(ViewState("Booking_no"), conn, tran)
        If (clsFc.InsertBooking_LogSheet(ViewState("Booking_no"), txtCS.Text, txtDraftFor.Text, txtDraftAFT.Text, txtIMO.Text, conn, tran) < 1) Then
            condition = False
        End If
        clsFc.DeleteBooking_LogSheet_Out(ViewState("Booking_no"), conn, tran)
        If (clsFc.InsertBooking_LogSheet_Out(ViewState("Booking_no"), txtPilotOut.Text, txtPOBOut.Text, txtTimePOBOut.Text, txtRemarkPOBOut.Text, txtTugRopingOut.Text, txtRemarkTugRopingOut.Text, txtLastLine.Text, txtTimeLastLine.Text, txtTimeLastLine.Text, txtRemarkLastLine.Text, txtNext.Text, txtRemarkNext.Text, txtEnd.Text, txtTimeEnd.Text, txtRemarkEnd.Text, txtAdditionOut.Text, User_Modify, "A", conn, tran) > 0) Then
            Dim dt As DataTable = ViewState("Tug")
            clsFc.DeleteBooking_LogSheet_Tug(ViewState("Booking_no"), "O", conn, tran)
            For Each dr As DataRow In dt.Rows
                If (clsFc.InsertBooking_LogSheet_Tug(ViewState("Booking_no"), dr.Item("Tug_Code"), dr.Item("Tug_Name"), dr.Item("Date_Time"), dr.Item("Start_Time"), dr.Item("End_Time"), dr.Item("Hrs"), dr.Item("Remark"), "O", dr.Item("Tug_Event"), conn, tran) < 1) Then
                    condition = False
                End If
            Next
            For i As Integer = 0 To gridProduct.Rows.Count - 1
                Dim txtArmConnect As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmConnect"), TextBox)
                Dim txtArmConnectFrom As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmConnectFrom"), TextBox)
                Dim txtArmConnectTo As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmConnectTo"), TextBox)
                Dim txtRemarkArmConnect As TextBox = CType(gridProduct.Rows(i).FindControl("txtRemarkArmConnect"), TextBox)

                Dim txtArmDis As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmDis"), TextBox)
                Dim txtArmDisFrom As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmDisFrom"), TextBox)
                Dim txtArmDisTo As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmDisTo"), TextBox)
                Dim txtRemarkArmDis As TextBox = CType(gridProduct.Rows(i).FindControl("txtRemarkArmDis"), TextBox)

                Dim txtETD As TextBox = CType(gridProduct.Rows(i).FindControl("txtETD"), TextBox)
                Dim txtETDTime As TextBox = CType(gridProduct.Rows(i).FindControl("txtETDTime"), TextBox)
                Dim txtRemarkETD As TextBox = CType(gridProduct.Rows(i).FindControl("txtRemarkETD"), TextBox)
                clsFc.DeleteBooking_LogSheet_Out_Product(ViewState("Booking_no"), gridProduct.DataKeys(i).Values("Product_no"), conn, tran)
                If (clsFc.InsertBooking_LogSheet_Out_Product(ViewState("Booking_no"), gridProduct.DataKeys(i).Values("Product_no"), txtArmConnect.Text, txtArmConnectFrom.Text, txtArmConnectTo.Text, txtRemarkArmConnect.Text, txtArmDis.Text, txtArmDisFrom.Text, txtArmDisTo.Text, txtRemarkArmDis.Text, txtETD.Text, txtETDTime.Text, txtRemarkETD.Text, conn, tran) < 1) Then
                    condition = False
                End If
            Next
        Else
            condition = False
        End If
        'If (clsFc.UPDATEBooking_LogSheet_OutStatus(ViewState("Booking_no"), "A", conn, tran) < 1) Then
        '    condition = False
        'End If
        If (clsFc.InsertAction_Operation(A_Code, ViewState("Booking_no"), "Approve Data By", DateTime.Now.ToString("dd MMMM yyyy", New CultureInfo("en-US")), DateTime.Now.ToString("HH:mm"), CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Name"), CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("User_name"), "", conn, tran) < 1) Then
            condition = False
        End If
        If dtLogSheet.Rows.Count > 0 Then
            If (dtLogSheet.Rows(0).Item("Status") & "" = "A") Then
                If (clsFc.UpdateBookingStatusOneField(ViewState("Booking_no"), "OA", conn, tran) < 1) Then
                    condition = False
                End If
            End If
        End If
        If (condition) Then
            clsDB.CommitTran(tran)
            Dim Send As String = ""
            Dim dtbooking As DataTable = clsGet.Get_BookingOneRecord(ViewState("Booking_no")).Tables(0)
            Dim dtLoad As DataTable = clsGet.Get_Load_Unload_Product(ViewState("Booking_no"), "")
            Dim Dis_Date As String = ""
            Dim Dis_Time As String = ""
            Dim Con_Date As String = ""
            Dim Con_Time As String = ""
            For Each dl As DataRow In dtLoad.Rows
                Try
                    If (Dis_Date = "") Then
                        Dis_Date = dl.Item("LU_Disconnect_Date") & ""
                        Dis_Time = dl.Item("LU_Disconnect_To") & ""
                    End If
                    Dim _DD As String = Dis_Date.Substring(0, 2) & " " & GetNameMonth(Dis_Date.Substring(3, 2)) & " " & CInt("20" & Dis_Date.Substring(6, 2))
                    Dim _D2 As String = dl.Item("LU_Disconnect_Date").Substring(0, 2) & " " & GetNameMonth(dl.Item("LU_Disconnect_Date").Substring(3, 2)) & " " & CInt("20" & dl.Item("LU_Disconnect_Date").Substring(6, 2))
                    If (CDate(_DD) < CDate(_D2)) Then
                        Dis_Date = dl.Item("LU_Disconnect_Date") & ""
                        Dis_Time = dl.Item("LU_Disconnect_To") & ""
                    ElseIf (CDate(_DD) = CDate(_D2)) Then
                        If (CInt(Dis_Time.Split(":")(0)) < CInt(dl.Item("LU_Disconnect_To").ToString.Split(":")(0))) Then
                            Dis_Date = dl.Item("LU_Disconnect_Date") & ""
                            Dis_Time = dl.Item("LU_Disconnect_To") & ""
                        ElseIf (CInt(Dis_Time.Split(":")(0)) = CInt(dl.Item("LU_Disconnect_To").ToString.Split(":")(0))) Then
                            Try
                                If (CInt(Dis_Time.Split(":")(1)) < CInt(dl.Item("LU_Disconnect_To").ToString.Split(":")(1))) Then
                                    Dis_Date = dl.Item("LU_Disconnect_Date") & ""
                                    Dis_Time = dl.Item("LU_Disconnect_To") & ""
                                End If
                            Catch ex As Exception

                            End Try
                        End If
                    End If

                    If (Con_Date = "") Then
                        Con_Date = dl.Item("LU_Connect_Date") & ""
                        Con_Time = dl.Item("LU_Connect_To") & ""
                    End If
                    _DD = Con_Date.Substring(0, 2) & " " & GetNameMonth(Con_Date.Substring(3, 2)) & " " & CInt("20" & Con_Date.Substring(6, 2))
                    _D2 = dl.Item("LU_Connect_Date").Substring(0, 2) & " " & GetNameMonth(dl.Item("LU_Connect_Date").Substring(3, 2)) & " " & CInt("20" & dl.Item("LU_Connect_Date").Substring(6, 2))
                    If (CDate(_DD) < CDate(_D2)) Then
                        Con_Date = dl.Item("LU_Connect_Date") & ""
                        Con_Time = dl.Item("LU_Connect_To") & ""
                    ElseIf (CDate(_DD) = CDate(_D2)) Then
                        If (CInt(Con_Time.Split(":")(0)) < CInt(dl.Item("LU_Connect_To").ToString.Split(":")(0))) Then
                            Con_Date = dl.Item("LU_Connect_Date") & ""
                            Con_Time = dl.Item("LU_Connect_To") & ""
                        ElseIf (CInt(Con_Time.Split(":")(0)) = CInt(dl.Item("LU_Connect_To").ToString.Split(":")(0))) Then
                            Try
                                If (CInt(Con_Time.Split(":")(1)) < CInt(dl.Item("LU_Connect_To").ToString.Split(":")(1))) Then
                                    Con_Date = dl.Item("LU_Connect_Date") & ""
                                    Con_Time = dl.Item("LU_Connect_To") & ""
                                End If
                            Catch ex As Exception

                            End Try
                        End If
                    End If
                Catch ex As Exception

                End Try

            Next
            Dim str As String = GenBody("เรือออกจากท่าเรือแล้ว", ViewState("Booking_no"), dtbooking.Rows(0).Item("MAgent_no") & "-" & dtbooking.Rows(0).Item("MAgent_name"), dtbooking.Rows(0).Item("Vehicle_no") & "-" & dtbooking.Rows(0).Item("Vehicle_Text"), dtbooking.Rows(0).Item("Date_From") & "-" & dtbooking.Rows(0).Item("Date_To"), Berth, txtLastLine.Text & " " & txtTimeLastLine.Text, Con_Date & " " & Con_Time, Dis_Date & " " & Dis_Time)
            Dim Formarketing As String = ""
            If (dtbooking.Rows(0).Item("PORT") = "L") Then
                Send = "LCT"
                Formarketing = "LCT"
            Else
                Send = "BCT"
                Formarketing = "BCT"
            End If
            If dtLogSheet.Rows.Count > 0 Then
                If (dtLogSheet.Rows(0).Item("Status") & "" = "A") Then
                    Send = "MARKETING"
                    str = GenBody("ดำเนินขนถ่ายสินค้าเสร็จสมบูรณ์ และ เรือออกจากท่าเรือแล้ว รอการคำนวณค่าใช้บริการจากระบบ iPORT", ViewState("Booking_no"), dtbooking.Rows(0).Item("MAgent_no") & "-" & dtbooking.Rows(0).Item("MAgent_name"), dtbooking.Rows(0).Item("Vehicle_no") & "-" & dtbooking.Rows(0).Item("Vehicle_Text"), dtbooking.Rows(0).Item("Date_From") & "-" & dtbooking.Rows(0).Item("Date_To"), Berth, txtLastLine.Text & " " & txtTimeLastLine.Text, Con_Date & " " & Con_Time, Dis_Date & " " & Dis_Time)
                End If
            End If
            SendMail(Send, str, ViewState("Booking_no"), Formarketing)
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('บันทึกการอนุมัติข้อมูลสำเร็จ');", True)
            LoadDataVesselLogSheetOUt()

        Else
            clsDB.RollBackTran(tran)
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึก');", True)
        End If
        Application.UnLock()
        conn.Dispose()
    End Sub

    Private Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
        'If (txtTimeLastLineFrom.Text.Trim = "") Then
        '    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุข้อมูล Last Line');", True)
        '    Exit Sub
        'ElseIf (txtTimeLastLineFrom.Text.Split(":")(0) > 23) Then
        '    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ข้อมูล Last Last ไม่ถูกต้อง');", True)
        '    Exit Sub
        'ElseIf (txtTimeLastLineFrom.Text.Split(":")(1) > 59) Then
        '    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ข้อมูล Last Last ไม่ถูกต้อง');", True)
        '    Exit Sub
        'End If
        If (txtTimeLastLine.Text.Trim = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุข้อมูล Last Line');", True)
            Exit Sub
        ElseIf (txtTimeLastLine.Text.Split(":")(0) > 23) Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ข้อมูล Last Last ไม่ถูกต้อง');", True)
            Exit Sub
        ElseIf (txtTimeLastLine.Text.Split(":")(1) > 59) Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ข้อมูล Last Last ไม่ถูกต้อง');", True)
            Exit Sub
        End If
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Dim User_Modify As String = CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("User_name")
        Application.Lock()
        conn.Open()
        Dim A_Code As String = clsDB.GeneratorID(conn, "Action_Operation")
        tran = clsDB.BeginTran(conn)
        clsFc.DeleteBooking_LogSheet(ViewState("Booking_no"), conn, tran)
        If (clsFc.InsertBooking_LogSheet(ViewState("Booking_no"), txtCS.Text, txtDraftFor.Text, txtDraftAFT.Text, txtIMO.Text, conn, tran) < 1) Then
            condition = False
        End If
        clsFc.DeleteBooking_LogSheet_Out(ViewState("Booking_no"), conn, tran)
        If (clsFc.InsertBooking_LogSheet_Out(ViewState("Booking_no"), txtPilotOut.Text, txtPOBOut.Text, txtTimePOBOut.Text, txtRemarkPOBOut.Text, txtTugRopingOut.Text, txtRemarkTugRopingOut.Text, txtLastLine.Text, txtTimeLastLine.Text, txtTimeLastLine.Text, txtRemarkLastLine.Text, txtNext.Text, txtRemarkNext.Text, txtEnd.Text, txtTimeEnd.Text, txtRemarkEnd.Text, txtAdditionOut.Text, User_Modify, "S", conn, tran) > 0) Then
            Dim dt As DataTable = ViewState("Tug")
            clsFc.DeleteBooking_LogSheet_Tug(ViewState("Booking_no"), "O", conn, tran)
            For Each dr As DataRow In dt.Rows
                If (clsFc.InsertBooking_LogSheet_Tug(ViewState("Booking_no"), dr.Item("Tug_Code"), dr.Item("Tug_Name"), dr.Item("Date_Time"), dr.Item("Start_Time"), dr.Item("End_Time"), dr.Item("Hrs"), dr.Item("Remark"), "O", dr.Item("Tug_Event"), conn, tran) < 1) Then
                    condition = False
                End If
            Next
            For i As Integer = 0 To gridProduct.Rows.Count - 1
                Dim txtArmConnect As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmConnect"), TextBox)
                Dim txtArmConnectFrom As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmConnectFrom"), TextBox)
                Dim txtArmConnectTo As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmConnectTo"), TextBox)
                Dim txtRemarkArmConnect As TextBox = CType(gridProduct.Rows(i).FindControl("txtRemarkArmConnect"), TextBox)

                Dim txtArmDis As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmDis"), TextBox)
                Dim txtArmDisFrom As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmDisFrom"), TextBox)
                Dim txtArmDisTo As TextBox = CType(gridProduct.Rows(i).FindControl("txtArmDisTo"), TextBox)
                Dim txtRemarkArmDis As TextBox = CType(gridProduct.Rows(i).FindControl("txtRemarkArmDis"), TextBox)

                Dim txtETD As TextBox = CType(gridProduct.Rows(i).FindControl("txtETD"), TextBox)
                Dim txtETDTime As TextBox = CType(gridProduct.Rows(i).FindControl("txtETDTime"), TextBox)
                Dim txtRemarkETD As TextBox = CType(gridProduct.Rows(i).FindControl("txtRemarkETD"), TextBox)
                clsFc.DeleteBooking_LogSheet_Out_Product(ViewState("Booking_no"), gridProduct.DataKeys(i).Values("Product_no"), conn, tran)
                If (clsFc.InsertBooking_LogSheet_Out_Product(ViewState("Booking_no"), gridProduct.DataKeys(i).Values("Product_no"), txtArmConnect.Text, txtArmConnectFrom.Text, txtArmConnectTo.Text, txtRemarkArmConnect.Text, txtArmDis.Text, txtArmDisFrom.Text, txtArmDisTo.Text, txtRemarkArmDis.Text, txtETD.Text, txtETDTime.Text, txtRemarkETD.Text, conn, tran) < 1) Then
                    condition = False
                End If
            Next
        Else
            condition = False
        End If
        'If (clsFc.UPDATEBooking_LogSheet_OutStatus(ViewState("Booking_no"), "S", conn, tran) < 1) Then
        '    condition = False
        'End If
        If (clsFc.InsertAction_Operation(A_Code, ViewState("Booking_no"), "Input Data By", DateTime.Now.ToString("dd MMMM yyyy", New CultureInfo("en-US")), DateTime.Now.ToString("HH:mm"), CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Name"), CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("User_name"), "", conn, tran) < 1) Then
            condition = False
        End If
        If (condition) Then
            clsDB.CommitTran(tran)
            LoadDataVesselLogSheetOUt()
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('บันทึกข้อมูลสำเร็จ');", True)
        Else
            clsDB.RollBackTran(tran)
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึก');", True)
        End If
        Application.UnLock()
        conn.Dispose()
    End Sub

    Private Sub btnReject_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReject.Click
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " document.getElementById('ctl00_contentplaceholder1_CancelPopup').src = '../FormPopup/Cancel_popup.aspx?Type=REJECT';", True)
        ModalPopupExtender7.Show()
    End Sub

    Private Sub btnRejectOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRejectOK.Click
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Dim User_Modify As String = CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("User_name")
        Application.Lock()
        conn.Open()
        Dim A_Code As String = clsDB.GeneratorID(conn, "Action_Operation")
        tran = clsDB.BeginTran(conn)

        If (clsFc.UPDATEBooking_LogSheet_OutStatus(ViewState("Booking_no"), "R", conn, tran) < 1) Then
            condition = False
        End If
        If (clsFc.InsertAction_Operation(A_Code, ViewState("Booking_no"), "Reject Data By", DateTime.Now.ToString("dd MMMM yyyy", New CultureInfo("en-US")), DateTime.Now.ToString("HH:mm"), CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Name"), CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("User_name"), Session("Cancel_Description") & "", conn, tran) < 1) Then
            condition = False
        End If
        If (condition) Then
            clsDB.CommitTran(tran)
            Session("Cancel_Description") = Nothing
            LoadBOoking()
            ViewState("Load") = "Not"
            Timer1_Tick(sender, e)
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('บันทึกข้อมูลสำเร็จ');", True)
        Else
            clsDB.RollBackTran(tran)
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึก');", True)
        End If
        Application.UnLock()
        conn.Dispose()
    End Sub


    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub

    Private Sub btnSearchVPS_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearchVPS.Click
        LoadBOoking()
        ViewState("Load") = "Not"
        Timer1_Tick(sender, e)
    End Sub

    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        PrintLogSheet()
    End Sub

    Private Sub btnPrintOut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrintOut.Click
        PrintLogSheet()
    End Sub

    Private Function CreateDS_Report() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("Commence", GetType(String))
        dt.Columns.Add("LastPort", GetType(String))
        dt.Columns.Add("ETA", GetType(String))
        dt.Columns.Add("Estimate", GetType(String))
        dt.Columns.Add("Start", GetType(String))
        dt.Columns.Add("AnchorTime", GetType(String))
        dt.Columns.Add("Position", GetType(String))
        dt.Columns.Add("TugRoping", GetType(String))
        dt.Columns.Add("AnchorUp", GetType(String))
        dt.Columns.Add("Pilot", GetType(String))
        dt.Columns.Add("POB", GetType(String))
        dt.Columns.Add("Schedule", GetType(String))
        dt.Columns.Add("Berthing", GetType(String))
        dt.Columns.Add("O_Arm", GetType(String))
        dt.Columns.Add("O_Commence", GetType(String))
        dt.Columns.Add("O_Commence_D", GetType(String))
        dt.Columns.Add("O_Complete", GetType(String))
        dt.Columns.Add("O_Complete_D", GetType(String))
        dt.Columns.Add("O_Dis", GetType(String))
        dt.Columns.Add("O_ETD", GetType(String))
        dt.Columns.Add("O_Pilot", GetType(String))
        dt.Columns.Add("O_POB", GetType(String))
        dt.Columns.Add("O_TugRoping", GetType(String))
        dt.Columns.Add("O_Last", GetType(String))
        dt.Columns.Add("O_Next", GetType(String))
        dt.Columns.Add("O_End", GetType(String))
        dt.Columns.Add("O_Commence2", GetType(String))
        dt.Columns.Add("O_Commence_D2", GetType(String))
        dt.Columns.Add("O_Commence3", GetType(String))
        dt.Columns.Add("O_Commence_D3", GetType(String))
        dt.Columns.Add("O_Commence4", GetType(String))
        dt.Columns.Add("O_Commence_D4", GetType(String))
        dt.Columns.Add("O_Complete2", GetType(String))
        dt.Columns.Add("O_Complete_D2", GetType(String))
        dt.Columns.Add("O_Complete3", GetType(String))
        dt.Columns.Add("O_Complete_D3", GetType(String))
        dt.Columns.Add("O_Complete4", GetType(String))
        dt.Columns.Add("O_Complete_D4", GetType(String))
        dt.Columns.Add("O_ETC", GetType(String))
        dt.Columns.Add("Remark1", GetType(String))
        dt.Columns.Add("Remark2", GetType(String))
        dt.Columns.Add("Remark3", GetType(String))
        dt.Columns.Add("Remark4", GetType(String))
        dt.Columns.Add("Remark5", GetType(String))
        dt.Columns.Add("Remark6", GetType(String))
        dt.Columns.Add("Remark7", GetType(String))
        dt.Columns.Add("Remark8", GetType(String))
        dt.Columns.Add("Remark9", GetType(String))
        dt.Columns.Add("Remark10", GetType(String))
        dt.Columns.Add("Remark11", GetType(String))
        dt.Columns.Add("Remark12", GetType(String))
        dt.Columns.Add("Remark13", GetType(String))
        dt.Columns.Add("Remark14", GetType(String))
        dt.Columns.Add("Remark15", GetType(String))
        dt.Columns.Add("Remark16", GetType(String))
        dt.Columns.Add("Remark17", GetType(String))
        dt.AcceptChanges()
        Return dt
    End Function

    Private Sub PrintLogSheet()
        Dim service As New _ReportingService.Report

        Dim optWindow As String = "left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no"
        Dim Orientation As Boolean = True
        Dim ReportName As String = String.Empty
        Dim reportDataSource As String = String.Empty
        Dim paraName As String = String.Empty
        Dim paraValue As String = String.Empty
        Dim reportType As String = String.Empty
        Dim urlDisplay As String = String.Empty
        Dim dsReport As New DataSet
        Dim dtN As DataTable = CreateDS_Report()
        Dim dr As DataRow = dtN.NewRow()
        Dim dtTug As DataTable = clsGet.Get_Booking_LogSheet_Tug(ViewState("Booking_no"), "I", "")
        Dim dt As DataTable = clsGet.Get_Booking_LogSheet_In(ViewState("Booking_no"))
        Dim Note As String = ""
        Dim O_Note As String = ""
        Dim Signal As String = ""
        Dim O_Signal As String = ""
        Dim tug1 As String = ""
        Dim S_Tug1 As String = ""
        Dim H_Tug1 As String = ""
        Dim R_Tug1 As String = ""
        Dim tug2 As String = ""
        Dim S_Tug2 As String = ""
        Dim H_Tug2 As String = ""
        Dim R_Tug2 As String = ""
        Dim tug3 As String = ""
        Dim S_Tug3 As String = ""
        Dim H_Tug3 As String = ""
        Dim R_Tug3 As String = ""
        Dim tug4 As String = ""
        Dim S_Tug4 As String = ""
        Dim H_Tug4 As String = ""
        Dim R_Tug4 As String = ""
        Dim tug5 As String = ""
        Dim S_Tug5 As String = ""
        Dim H_Tug5 As String = ""
        Dim R_Tug5 As String = ""
        Dim tug6 As String = ""
        Dim S_Tug6 As String = ""
        Dim H_Tug6 As String = ""
        Dim R_Tug6 As String = ""
        Dim tug7 As String = ""
        Dim S_Tug7 As String = ""
        Dim H_Tug7 As String = ""
        Dim R_Tug7 As String = ""
        Dim tug8 As String = ""
        Dim S_Tug8 As String = ""
        Dim H_Tug8 As String = ""
        Dim R_Tug8 As String = ""
        Dim tug9 As String = ""
        Dim S_Tug9 As String = ""
        Dim H_Tug9 As String = ""
        Dim R_Tug9 As String = ""
        Dim tug10 As String = ""
        Dim S_Tug10 As String = ""
        Dim H_Tug10 As String = ""
        Dim R_Tug10 As String = ""
        If (dt.Rows.Count > 0) Then
            If (dt.Rows(0).Item("Commence_Date") & "" <> "") Then
                dr.Item("Commence") = dt.Rows(0).Item("Commence_Date") & "/" & dt.Rows(0).Item("Commence_Time")
            End If

            dr.Item("Remark1") = dt.Rows(0).Item("Commence_Remark")
            dr.Item("LastPort") = dt.Rows(0).Item("Last_Value")
            dr.Item("Remark2") = dt.Rows(0).Item("Last_Remark")
            If (dt.Rows(0).Item("ETA_Date") & "" <> "") Then
                dr.Item("ETA") = dt.Rows(0).Item("ETA_Date") & "/" & dt.Rows(0).Item("ETA_Time")
            End If
            dr.Item("Remark3") = dt.Rows(0).Item("ETA_Remark")
            dr.Item("Start") = dt.Rows(0).Item("Start_Value")
            dr.Item("Remark5") = dt.Rows(0).Item("Start_Remark")
            If (dt.Rows(0).Item("Anchor_Date") & "" <> "") Then
                dr.Item("AnchorTime") = dt.Rows(0).Item("Anchor_Date") & "/" & dt.Rows(0).Item("Anchor_Time")
            End If

            dr.Item("Remark6") = dt.Rows(0).Item("Anchor_Remark")
            dr.Item("Position") = dt.Rows(0).Item("Position_Value")
            dr.Item("Remark7") = dt.Rows(0).Item("Position_Remark")
            dr.Item("TugRoping") = dt.Rows(0).Item("Tug_Value")
            dr.Item("Remark8") = dt.Rows(0).Item("Tug_Remark")
            dr.Item("Pilot") = dt.Rows(0).Item("Pilot")
            If (dt.Rows(0).Item("POB_Date") & "" <> "") Then
                dr.Item("POB") = dt.Rows(0).Item("POB_Date") & "/" & dt.Rows(0).Item("POB_Time")
            End If

            dr.Item("Remark9") = dt.Rows(0).Item("POB_Remark")
            If (dt.Rows(0).Item("Schedule_Date") & "" <> "") Then
                dr.Item("Schedule") = dt.Rows(0).Item("Schedule_Date") & "/" & dt.Rows(0).Item("Schedule_Time")
            End If
            dr.Item("Remark10") = ""
            dr.Item("Remark11") = dt.Rows(0).Item("Schedule_Remark")
            If (dt.Rows(0).Item("Berthing_Date") & "" <> "") Then
                dr.Item("Berthing") = dt.Rows(0).Item("Berthing_Date") & "/" & dt.Rows(0).Item("Berthing_TimeFrom") & "-" & dt.Rows(0).Item("Berthing_TimeTo")
            End If

            dr.Item("Remark12") = dt.Rows(0).Item("Berthing_Remark")
            Note = dt.Rows(0).Item("Addition")
            If (dt.Rows(0).Item("NOR_Date") & "" <> "") Then
                dr.Item("Estimate") = dt.Rows(0).Item("NOR_Date") & "/" & dt.Rows(0).Item("NOR_Time")
            End If
            dr.Item("Remark4") = ""
            If (dt.Rows(0).Item("Up_Date") & "" <> "") Then
                dr.Item("AnchorUp") = dt.Rows(0).Item("Up_Date") & "/" & dt.Rows(0).Item("Up_Time")
            End If

            Dim User_Modify As String = dt.Rows(0).Item("User_Modify")
            lbSIGNATURE.Text = clsGet.Get_NameUser(User_Modify.Split("$")(0))
            Try
                lbSIGNATURE.Text &= User_Modify.Split("$")(1)
            Catch ex As Exception

            End Try
            Signal = lbSIGNATURE.Text
            For Each drX As DataRow In dtTug.Rows
                If (drX.Item("Tug_Code") & "" <> "") Then
                    If (tug1 <> "") Then
                        If (tug2 <> "") Then
                            If (tug3 <> "") Then
                                If (tug4 <> "") Then
                                    If (tug5 = "") Then
                                        tug5 = drX.Item("Tug_Name")
                                        S_Tug5 = drX.Item("Date_Time") & "/" & drX.Item("Start_Time") & "-" & drX.Item("End_Time")
                                        H_Tug5 = drX.Item("Hrs")
                                        R_Tug5 = drX.Item("Remark")
                                    End If
                                Else
                                    tug4 = drX.Item("Tug_Name")
                                    S_Tug4 = drX.Item("Date_Time") & "/" & drX.Item("Start_Time") & "-" & drX.Item("End_Time")
                                    H_Tug4 = drX.Item("Hrs")
                                    R_Tug4 = drX.Item("Remark")
                                End If
                            Else
                                tug3 = drX.Item("Tug_Name")
                                S_Tug3 = drX.Item("Date_Time") & "/" & drX.Item("Start_Time") & "-" & drX.Item("End_Time")
                                H_Tug3 = drX.Item("Hrs")
                                R_Tug3 = drX.Item("Remark")
                            End If
                        Else
                            tug2 = drX.Item("Tug_Name")
                            S_Tug2 = drX.Item("Date_Time") & "/" & drX.Item("Start_Time") & "-" & drX.Item("End_Time")
                            H_Tug2 = drX.Item("Hrs")
                            R_Tug2 = drX.Item("Remark")
                        End If
                    Else
                        tug1 = drX.Item("Tug_Name")
                        S_Tug1 = drX.Item("Date_Time") & "/" & drX.Item("Start_Time") & "-" & drX.Item("End_Time")
                        H_Tug1 = drX.Item("Hrs")
                        R_Tug1 = drX.Item("Remark")
                    End If
                End If
            Next
        End If
        dtTug = clsGet.Get_Booking_LogSheet_Tug(ViewState("Booking_no"), "O", "")
        dt = clsGet.Get_Load_Unload_Vessel(ViewState("Booking_no"))
        If (dt.Rows.Count > 0) Then
            If (dt.Rows(0).Item("Depart_Actual_Date") & "" <> "") Then
                dr.Item("O_Last") = dt.Rows(0).Item("Depart_Actual_Date") & "/" & dt.Rows(0).Item("Depart_Actual_Time") & ""
            End If

        End If
        dt = clsGet.Get_Booking_LogSheet_Out(ViewState("Booking_no"))
        If (dt.Rows.Count > 0) Then
            dr.Item("O_Pilot") = dt.Rows(0).Item("Pilot")
            If (dt.Rows(0).Item("POB_Date") & "" <> "") Then
                dr.Item("O_POB") = dt.Rows(0).Item("POB_Date") & "/" & dt.Rows(0).Item("POB_Time")
            End If

            dr.Item("Remark13") = dt.Rows(0).Item("POB_Remark")
            dr.Item("O_TugRoping") = dt.Rows(0).Item("Tug_Value")
            dr.Item("Remark14") = dt.Rows(0).Item("Tug_Remark")
            If (dt.Rows(0).Item("Last_Date") & "" <> "") Then
                dr.Item("O_Last") = dt.Rows(0).Item("Last_Date") & "/" & dt.Rows(0).Item("Last_Time")
            End If

            'txtTimeLastLineFrom.Text = dt.Rows(0).Item("Last_TimeFrom") & ""
            dr.Item("Remark15") = dt.Rows(0).Item("Last_Remark")
            dr.Item("O_Next") = dt.Rows(0).Item("Next_Value")
            dr.Item("Remark16") = dt.Rows(0).Item("Next_Remark")
            If (dt.Rows(0).Item("End_Date") & "" <> "") Then
                dr.Item("O_End") = dt.Rows(0).Item("End_Date") & "/" & dt.Rows(0).Item("End_Time")
            End If

            dr.Item("Remark17") = dt.Rows(0).Item("End_Remark")
            O_Note = dt.Rows(0).Item("Addition")
        End If
        For Each drX As DataRow In dtTug.Rows
            If (drX.Item("Tug_Code") & "" <> "") Then
                If (tug6 <> "") Then
                    If (tug7 <> "") Then
                        If (tug8 <> "") Then
                            If (tug9 <> "") Then
                                If (tug10 = "") Then
                                    tug10 = drX.Item("Tug_Name")
                                    S_Tug10 = drX.Item("Date_Time") & "/" & drX.Item("Start_Time") & "-" & drX.Item("End_Time")
                                    H_Tug10 = drX.Item("Hrs")
                                    R_Tug10 = drX.Item("Remark")
                                End If
                            Else
                                tug9 = drX.Item("Tug_Name")
                                S_Tug9 = drX.Item("Date_Time") & "/" & drX.Item("Start_Time") & "-" & drX.Item("End_Time")
                                H_Tug9 = drX.Item("Hrs")
                                R_Tug9 = drX.Item("Remark")
                            End If
                        Else
                            tug8 = drX.Item("Tug_Name")
                            S_Tug8 = drX.Item("Date_Time") & "/" & drX.Item("Start_Time") & "-" & drX.Item("End_Time")
                            H_Tug8 = drX.Item("Hrs")
                            R_Tug8 = drX.Item("Remark")
                        End If
                    Else
                        tug7 = drX.Item("Tug_Name")
                        S_Tug7 = drX.Item("Date_Time") & "/" & drX.Item("Start_Time") & "-" & drX.Item("End_Time")
                        H_Tug7 = drX.Item("Hrs")
                        R_Tug7 = drX.Item("Remark")
                    End If
                Else
                    tug6 = drX.Item("Tug_Name")
                    S_Tug6 = drX.Item("Date_Time") & "/" & drX.Item("Start_Time") & "-" & drX.Item("End_Time")
                    H_Tug6 = drX.Item("Hrs")
                    R_Tug6 = drX.Item("Remark")
                End If
            End If
        Next
        dt = clsGet.Get_Load_Unload_Product(ViewState("Booking_no"), "")
        Dim Commence1 As String = ""
        Dim Commence_D1 As String = ""
        Dim Commence2 As String = ""
        Dim Commence_D2 As String = ""
        Dim Commence3 As String = ""
        Dim Commence_D3 As String = ""
        Dim Commence4 As String = ""
        Dim Commence_D4 As String = ""
        If (dt.Rows.Count > 0) Then
            For i As Integer = 0 To dt.Rows.Count - 1
                Dim tmp As DataTable = clsGet.Get_Booking_Product_LogSheetOut(ViewState("Booking_no") & "")
                If (tmp.Rows.Count > 0) Then
                    For Each ss As DataRow In tmp.Rows
                        If (ss.Item("Product_no") = dt.Rows(i).Item("Product_no")) Then
                            If (i = 0) Then
                                'Dim Arm As String = ""
                                'If (tmp.Rows(0).Item("Connect_Date") & "" <> "") Then
                                '    Try
                                '        Arm = tmp.Rows(0).Item("Connect_Date").ToString.Substring(0, 2) & " " & GetNameMonth(tmp.Rows(0).Item("Connect_Date").ToString.Substring(3, 2)) & " 20" & tmp.Rows(0).Item("Connect_Date").ToString.Substring(6, 2)
                                '    Catch ex As Exception

                                '    End Try
                                'End If
                                dr.Item("O_Arm") = tmp.Rows(0).Item("Connect_Date") & "/" & tmp.Rows(0).Item("Connect_TimeFrom") & "-" & tmp.Rows(0).Item("Connect_TimeTo")
                            ElseIf (i = dt.Rows.Count - 1) Then
                                'Dim dis As String = ""
                                'If (tmp.Rows(0).Item("Dis_Date") & "" <> "") Then
                                '    Try
                                '        dis = tmp.Rows(0).Item("Dis_Date").ToString.Substring(0, 2) & " " & GetNameMonth(tmp.Rows(0).Item("Dis_Date").ToString.Substring(3, 2)) & " 20" & tmp.Rows(0).Item("Dis_Date").ToString.Substring(6, 2)
                                '    Catch ex As Exception

                                '    End Try
                                'End If
                                dr.Item("O_Dis") = tmp.Rows(0).Item("Dis_Date") & "/" & tmp.Rows(0).Item("Dis_TimeFrom") & "-" & tmp.Rows(0).Item("Dis_TimeTo")
                                'If (tmp.Rows(0).Item("ETD_Date") & "" <> "") Then
                                '    Try
                                '        dis = tmp.Rows(0).Item("ETD_Date").ToString.Substring(0, 2) & " " & GetNameMonth(tmp.Rows(0).Item("ETD_Date").ToString.Substring(3, 2)) & " 20" & tmp.Rows(0).Item("ETD_Date").ToString.Substring(6, 2)
                                '    Catch ex As Exception

                                '    End Try
                                'End If
                                dr.Item("O_ETD") = tmp.Rows(0).Item("ETD_Date") & "/" & tmp.Rows(0).Item("ETD_Time")
                            End If
                        End If
                    Next
                End If
                If (i = 0) Then
                    Dim t As String = ""
                    If (dt.Rows(i).Item("LU_StopStandard_Date") & "" <> "") Then
                        Try
                            t = dt.Rows(i).Item("LU_StopStandard_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dt.Rows(i).Item("LU_StopStandard_Date").ToString.Substring(3, 2)) & " 20" & dt.Rows(i).Item("LU_StopStandard_Date").ToString.Substring(6, 2)
                        Catch ex As Exception

                        End Try
                    End If
                    dr.Item("O_ETC") = t & "/" & dt.Rows(i).Item("LU_StopStandard_Time")
                End If
                Dim O_Commence_D As String = ""
                Dim O_Commence_D2 As String = ""
                If (dt.Rows(i).Item("LU_StartActual_Date") & "" <> "") Then
                    Try
                        O_Commence_D = dt.Rows(i).Item("LU_StartActual_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dt.Rows(i).Item("LU_StartActual_Date").ToString.Substring(3, 2)) & " 20" & dt.Rows(i).Item("LU_StartActual_Date").ToString.Substring(6, 2)
                    Catch ex As Exception

                    End Try
                End If
                If (dt.Rows(i).Item("LU_StopActual_Date") & "" <> "") Then
                    Try
                        O_Commence_D2 = dt.Rows(i).Item("LU_StopActual_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dt.Rows(i).Item("LU_StopActual_Date").ToString.Substring(3, 2)) & " 20" & dt.Rows(i).Item("LU_StopActual_Date").ToString.Substring(6, 2)
                    Catch ex As Exception

                    End Try
                End If
                If (Commence1 <> "") Then
                    If (Commence2 <> "") Then
                        If (Commence3 <> "") Then
                            If (Commence4 = "") Then
                                dr.Item("O_Commence4") = dt.Rows(i).Item("Product_name") & ""
                                dr.Item("O_Commence_D4") = O_Commence_D & "/" & dt.Rows(i).Item("LU_StartActual_Time")
                                dr.Item("O_Complete4") = dt.Rows(i).Item("Product_name") & ""
                                dr.Item("O_Complete_D4") = O_Commence_D2 & "/" & dt.Rows(i).Item("LU_StopActual_Time")
                            End If
                        Else
                            dr.Item("O_Commence3") = dt.Rows(i).Item("Product_name") & ""
                            dr.Item("O_Commence_D3") = O_Commence_D & "/" & dt.Rows(i).Item("LU_StartActual_Time")
                            dr.Item("O_Complete3") = dt.Rows(i).Item("Product_name") & ""
                            dr.Item("O_Complete_D3") = O_Commence_D2 & "/" & dt.Rows(i).Item("LU_StopActual_Time")
                        End If
                    Else
                        dr.Item("O_Commence2") = dt.Rows(i).Item("Product_name") & ""
                        dr.Item("O_Commence_D2") = O_Commence_D & "/" & dt.Rows(i).Item("LU_StartActual_Time")
                        dr.Item("O_Complete2") = dt.Rows(i).Item("Product_name") & ""
                        dr.Item("O_Complete_D2") = O_Commence_D2 & "/" & dt.Rows(i).Item("LU_StopActual_Time")
                    End If
                Else
                    dr.Item("O_Commence") = dt.Rows(i).Item("Product_name") & ""
                    dr.Item("O_Commence_D") = O_Commence_D & "/" & dt.Rows(i).Item("LU_StartActual_Time")
                    dr.Item("O_Complete") = dt.Rows(i).Item("Product_name") & ""
                    dr.Item("O_Complete_D") = O_Commence_D2 & "/" & dt.Rows(i).Item("LU_StopActual_Time")
                End If
            Next
        End If
        dtN.Rows.Add(dr)

        dsReport.Tables.Add(dtN.Copy)
        dsReport.AcceptChanges()
        ReportName = "Vessel_Log_Sheet.rdlc"
        reportDataSource = "VesselLog_Value"
        dt = clsGet.Get_Booking_LogSheet(ViewState("Booking_no"))
        Dim dtOut As DataTable = clsGet.Get_SignalOUT(ViewState("Booking_no"))
        If (dtOut.Rows.Count > 0) Then
            Dim A_Date As String = dtOut.Rows(0).Item("A_Date").ToString.Split(" ")(2)
            Try
                A_Date = A_Date.Substring(2, 2)
            Catch ex As Exception
                A_Date = Date.Now.ToString("yy")
            End Try

            O_Signal = dtOut.Rows(0).Item("A_By") & " วันที่ : " & CDate(dtOut.Rows(0).Item("A_Date")).ToString("dd/MM/") & A_Date & " " & dtOut.Rows(0).Item("A_Time")
        End If
        Dim CS As String = ""
        Dim D_FOR As String = ""
        Dim D_AFT As String = ""
        If (dt.Rows.Count > 0) Then
            CS = "C/S" & dt.Rows(0).Item("C_S") & ""
            D_FOR = "DRAFT :FOR " & dt.Rows(0).Item("Draft_FOR") & " M"
            D_AFT = "AFT : " & dt.Rows(0).Item("Draft_AFT") & " M"
            ' txtIMO.Text = dt.Rows(0).Item("IMO_no") & ""
        End If
        paraName = "Name;CS;FLAG;DRAFT;AFT;LOA;BERTH;AGENT;SHIFT;Note;Signal;O_Note;O_Signal;Tug1;S_Tug1;H_Tug1;R_Tug1;Tug2;S_Tug2;H_Tug2;R_Tug2;Tug3;S_Tug3;H_Tug3;R_Tug3;Tug4;S_Tug4;H_Tug4;R_Tug4;Tug5;S_Tug5;H_Tug5;R_Tug5;Tug6;S_Tug6;H_Tug6;R_Tug6;Tug7;S_Tug7;H_Tug7;R_Tug7;Tug8;S_Tug8;H_Tug8;R_Tug8;Tug9;S_Tug9;H_Tug9;R_Tug9;Tug10;S_Tug10;H_Tug10;R_Tug10"
        paraValue = "NAME :" & lbName.Text & ";" & CS & ";FLAG :" & lbFlag.Text & ";" & D_FOR & ";" & D_AFT & ";" & lbLOA.Text & ";WHARF NO :" & lbWharf.Text & ";AGENT :" & lbAgent.Text & ";;" & Note & ";" & Signal & ";" & O_Note & ";" & O_Signal & ";" & tug1 & ";" & S_Tug1 & ";" & H_Tug1 & ";" & R_Tug1 & ";" & tug2 & ";" & S_Tug2 & ";" & H_Tug2 & ";" & R_Tug2 & ";" & tug3 & ";" & S_Tug3 & ";" & H_Tug3 & ";" & R_Tug3 & ";" & tug4 & ";" & S_Tug4 & ";" & H_Tug4 & ";" & R_Tug4 & ";" & tug5 & ";" & S_Tug5 & "; " & H_Tug5 & ";" & R_Tug5 & ";" & tug6 & ";" & S_Tug6 & ";" & H_Tug6 & ";" & R_Tug6 & ";" & tug7 & ";" & S_Tug7 & ";" & H_Tug7 & ";" & R_Tug7 & ";" & tug8 & ";" & S_Tug8 & ";" & H_Tug8 & ";" & R_Tug8 & ";" & tug9 & ";" & S_Tug9 & ";" & H_Tug9 & ";" & R_Tug9 & ";" & tug10 & ";" & S_Tug10 & ";" & H_Tug10 & ";" & R_Tug10
        Orientation = True
        reportType = "pdf"

        urlDisplay = service.CreateReporting(dsReport, reportDataSource, reportType, ReportName, paraName, paraValue, Orientation, "")

        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.open('" + urlDisplay + "','_blank','" + optWindow + "');", True)
    End Sub

    Private Function GenBody(ByVal str As String, ByVal booking_no As String, ByVal Agent As String, ByVal Vessel As String, ByVal Laycan As String, ByVal Berth As String, ByVal Depart As String, ByVal Start As String, ByVal _Stop As String) As String
        Dim HTML As String = ""
        HTML = "<table width='100%' border='0' cellpadding='2' cellspacing='1'>"
        HTML &= "<tr>"
        HTML &= "<td align='left'><font class='White-Style' font-weight:bold style='color:#000000'>" & str & "</font></td>"
        HTML &= "</tr>"
        HTML &= "</table>"

        HTML &= "<table width='100%' border='0' cellpadding='2' cellspacing='1'>"
        HTML &= "<tr>"
        HTML &= "<td style='width:220px;' align='right'><font class='White-Style' style='color:#000000'>Booking No. :</font>&nbsp;</td>"
        HTML &= "<td align='left'><font class='White-Style' style='color:#000000'>" & booking_no & "</font></td>"
        HTML &= "</tr>"
        HTML &= "<tr>"
        HTML &= "<td style='width:220px;' align='right'><font class='White-Style' style='color:#000000'>Sold To :</font>&nbsp;</td>"
        HTML &= "<td align='left'><font class='White-Style' style='color:#000000'>" & Agent & "</font></td>"
        HTML &= "</tr>"
        HTML &= "<tr>"
        HTML &= "<td style='width:220px;' align='right'><font class='White-Style' style='color:#000000'>Vessel :</font>&nbsp;</td>"
        HTML &= "<td align='left'><font class='White-Style' style='color:#000000'>" & Vessel & "</font></td>"
        HTML &= "</tr>"
        HTML &= "<tr>"
        HTML &= "<td style='width:220px;' align='right'><font class='White-Style' style='color:#000000'>Lay Can\Lay Day :</font>&nbsp;</td>"
        HTML &= "<td align='left'><font class='White-Style' style='color:#000000'>" & Laycan & "</font></td>"
        HTML &= "</tr>"
        HTML &= "<tr>"
        HTML &= "<td style='width:220px;' align='right'><font class='White-Style' style='color:#000000'>Berth :</font>&nbsp;</td>"
        HTML &= "<td align='left'><font class='White-Style' style='color:#000000'>" & Berth & "</font></td>"
        HTML &= "</tr>"
        HTML &= "<tr>"
        HTML &= "<td style='width:220px;' align='right'><font class='White-Style' style='color:#000000'>Start S/U S/L :</font>&nbsp;</td>"
        HTML &= "<td align='left'><font class='White-Style' style='color:#000000'>" & Start & "</font></td>"
        HTML &= "</tr>"
        HTML &= "<tr>"
        HTML &= "<td style='width:220px;' align='right'><font class='White-Style' style='color:#000000'>Stop S/U S/L :</font>&nbsp;</td>"
        HTML &= "<td align='left'><font class='White-Style' style='color:#000000'>" & _Stop & "</font></td>"
        HTML &= "</tr>"
        HTML &= "<tr>"
        HTML &= "<td style='width:220px;' align='right'><font class='White-Style' style='color:#000000'>Depart :</font>&nbsp;</td>"
        HTML &= "<td align='left'><font class='White-Style' style='color:#000000'>" & Depart & "</font></td>"
        HTML &= "</tr>"
        HTML &= "</table>"
        Return HTML
    End Function

    Private Function SendMail(ByVal SEND As String, ByVal Body As String, ByVal Booking_no As String, ByVal FORMARKETING As String) As Boolean
        If SEND = "LCT" Then
            Dim test As String = clsFc.sendmail(clsGet.Get_EmailForSend("'00004','00011','00017','00018'") & ";" & clsGet.Get_EmailForSendAgent(Booking_no), "", "iPORT", Body, "")
            If (test <> "") Then
                Return True
            Else
                Return False
            End If
        ElseIf SEND = "BCT" Then
            Dim test As String = clsFc.sendmail(clsGet.Get_EmailForSend("'00004','00010','00015','00016'") & ";" & clsGet.Get_EmailForSendAgent(Booking_no), "", "iPORT", Body, "")
            If (test <> "") Then
                Return True
            Else
                Return False
            End If
        ElseIf SEND = "MARKETING" Then
            Dim test As String = ""
            If FORMARKETING = "BCT" Then
                test = clsFc.sendmail(clsGet.Get_EmailForSend("'00003','00004','00010','00015','00016'") & ";" & clsGet.Get_EmailForSendAgent(Booking_no), "", "iPORT", Body, "")
            Else
                test = clsFc.sendmail(clsGet.Get_EmailForSend("'00003','00004','00011','00017','00018'") & ";" & clsGet.Get_EmailForSendAgent(Booking_no), "", "iPORT", Body, "")
            End If

            If (test <> "") Then
                Return True
            Else
                Return False
            End If
        End If
    End Function
End Class